通过一个Wordpress插件,我有了一个收集用户值并将其与存储在Google工作表中的值进行比较的表单。
设置:类的PHP文件有一个连接到Google客户端的结构,并为要使用的类方法分配一个变量。这些方法获取数据并比较数据。当用户填写并提交表单时,AJAX调用将值发送到php文件,在该文件中将启动此类的新实例,并通过类方法将表单值与Google表的值进行比较。
问题:在我使用Wordpress之前,这一切都像预期的那样工作。当我将ajax url设置为admin-ajax.php并使用wp_ajax_{my_function}/ wp_ajax_nopriv_{my_function}挂接时,它不起作用。经过一些挖掘,这是我在Wordpress调试日志中发现的问题:
PHP Fatal error: Uncaught InvalidArgumentException: file "google-api/credentials.json" does not exist in wordpress\wp-content\plugins\myplugin\google-api\php-client-2.4.0\src\Google\Client.php:904
Stack trace:
#0 wordpress\wp-content\plugins\myplugin\my-class.php(37): Google_Client->setAuthConfig('google-api/cred...')
#1 wordpress\wp-content\plugins\myplugin\my-class.php\my-php-file.php(25): MyObject->__construct()
#2wordpress\wp-content\plugins\myplugin\FileContainingMyAjaxHookFunction.php: require_once('my-class.php')
#3 C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php(288): MyAddActionsObject->MyAjaxHookFunction('')
#4 wordpress\wp-includes\class-wp-hook.php(312): WP_Hook->apply_filters('', Array)
#5 wordpress\wp-includes\plugin.php(478): WP_Hook->do_action(Array)
#6 C in C:\xampp\htdocs\wordpress\wp-content\plugins\myplugin\google-api\php-client-2.4.0\src\Google\Client.php on line 904当我在AJAX调用的url中直接调用php文件时,没有任何问题。这让我相信这是我通过Wordpress钩子处理AJAX的方式。
我的类看起来像这样:
require_once 'google-api/php-client-2.4.0/vendor/autoload.php';
class My_Object {
protected static $googleService;
public function __construct() {
$client = new \Google_Client();
$client->setApplicationName ('My Name');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig('google-api/credentials.json');
self::$googleService = new Google_Service_Sheets($client);
}
public static function get_sheet_data() {
$service = self::$googleService;
}
public static function do_stuff() {...}
} Ajax:
$.ajax( {
type: "POST",
url: myAjax.ajaxurl,
data: {
action: 'MyAjaxHookFunction',
myDataOne: myDataOne,
myDataTwo: myDataTwo
},
success: function(results) {
$("#MyId").append(results);
jQuery( document.body ).trigger( 'post-load' );
}
});MyAjaxHookFunction:
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'my-php-file.php';
die();注意:我尝试在MyAjaxHookFunction中添加类实例,但没有成功。
我的php文件:
require_once ('my-class.php');
$data = new My_Object;
$data::dostuff();注意:我在本地通过XAMPP工作。Google PHP API包含在插件目录中。我使用PHP,因为我不希望数据存储在Google Sheets中可见。
任何帮助或洞察力都很感谢。谢谢。
发布于 2020-04-15 17:52:58
睡了一觉,再看一遍,解决方案看起来很简单。
出现问题的原因: AJAX是通过wp-include目录中的admin-ajax.php执行的。我的类构造引用了一个相对于类文件本身位置的凭证文件。我需要指定一个可以从目录中的任何位置使用的路径。
为了解决这个问题,我在类结构中编辑了$client->setAuthConfig(' url ')中的url参数:
require_once 'google-api/php-client-2.4.0/vendor/autoload.php';
class My_Object {
protected static $googleService;
public function __construct() {
$client = new \Google_Client();
$client->setApplicationName ('My Name');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig( plugin_dir_path(__FILE__) . 'google-api/credentials.json');
self::$googleService = new Google_Service_Sheets($client);
}
public static function get_sheet_data() {
$service = self::$googleService;
}
public static function do_stuff() {...}
} 现在它可以正常工作了。
经验教训:在插件文件中指定路径时,请始终使用Wordpress目录路径函数,如plugin_dir_path()。
如果我的实现或更好的方式有任何问题,我期待着听到他们。
https://stackoverflow.com/questions/61215172
复制相似问题