From d93ebbe881c842a604cead45824a4f5f8d888c01 Mon Sep 17 00:00:00 2001 From: Sapayth Hossain Date: Tue, 20 Jun 2023 11:39:32 +0600 Subject: [PATCH 1/2] AjaxTrait for registering ajax actions --- src/AjaxTrait.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/AjaxTrait.php diff --git a/src/AjaxTrait.php b/src/AjaxTrait.php new file mode 100644 index 0000000..d1c9573 --- /dev/null +++ b/src/AjaxTrait.php @@ -0,0 +1,38 @@ + false ] ); // for logged-in users only + * register_ajax( 'action', 'action_callback', [ 'nopriv' => true, 'priv' => false ] ); // for logged-out users only + * + * @param string $action + * @param callable|string $callback + * @param array $args + * + * @return void + */ + public function register_ajax( $action, $callback, $args = [] ) { + $default = [ + 'prefix' => '', // it is always a good idea to prefix actions to make it unique. + 'nopriv' => true, + 'priv' => true, + ]; + + $args = wp_parse_args( $default, $args ); + + if ( $args['priv'] ) { + add_action( 'wp_ajax' . $args['prefix'] . $action, $callback ); + } + + if ( $args['nopriv'] ) { + add_action( 'wp_ajax_nopriv' . $args['prefix'] . $action, $callback ); + } + } +} From cd682dc0a03b217dc419d5dad56b7472998e836c Mon Sep 17 00:00:00 2001 From: Sapayth Hossain Date: Fri, 23 Jun 2023 13:03:21 +0600 Subject: [PATCH 2/2] update readme, helper properties added --- README.md | 24 ++++++++++++++++++++++++ src/AjaxTrait.php | 25 ++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b6e8085..963b0d1 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,30 @@ $instance->doSomething(); ``` +### AjaxTrait + +The `AjaxTrait` provides a function to create the WordPress AJAX actions easily. No need to add actions for logged-in and logged-out users separately. + +Usage example: + +```php +use WeDevs\WpUtils\AjaxTrait; + +class MyAjaxClass { + + use AjaxTrait; + + public function __construct() { + // Register an AJAX action. + // This will produce: + // add_action( 'wp_ajax_yourproject_submit_form, [ $this, 'submit_form_callback' ] ); + // add_action( 'wp_ajax_nopriv_yourproject_submit_form, [ $this, 'submit_form_callback' ] ); + $this->register_ajax( 'yourproject_submit_form', [ $this, 'submit_form_callback' ] ); + } +} + +``` + ## License diff --git a/src/AjaxTrait.php b/src/AjaxTrait.php index d1c9573..5ac9caa 100644 --- a/src/AjaxTrait.php +++ b/src/AjaxTrait.php @@ -4,6 +4,20 @@ trait AjaxTrait { + /** + * A predefined array to use when we need to create AJAX actions only for logged in users + * + * @var array + */ + protected $logged_in_only = [ 'nopriv' => false ]; + + /** + * A predefined array to use when we need to create AJAX actions only for logged out users + * + * @var array + */ + protected $logged_out_only = [ 'nopriv' => true, 'priv' => false ]; + /** * Register ajax into action hook * @@ -20,19 +34,20 @@ trait AjaxTrait { */ public function register_ajax( $action, $callback, $args = [] ) { $default = [ - 'prefix' => '', // it is always a good idea to prefix actions to make it unique. - 'nopriv' => true, - 'priv' => true, + 'nopriv' => true, + 'priv' => true, + 'priority' => 10, + 'accepted_args' => 1, ]; $args = wp_parse_args( $default, $args ); if ( $args['priv'] ) { - add_action( 'wp_ajax' . $args['prefix'] . $action, $callback ); + add_action( 'wp_ajax_' . $action, $callback, $args['priority'], $args['accepted_args'] ); } if ( $args['nopriv'] ) { - add_action( 'wp_ajax_nopriv' . $args['prefix'] . $action, $callback ); + add_action( 'wp_ajax_nopriv_' . $action, $callback, $args['priority'], $args['accepted_args'] ); } } }