| @@ -1,0 +1,216 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace NotificationX\Core\Rest; | |
| 4 | + | |
| 5 | +use NotificationX\Admin\Admin; | |
| 6 | +use NotificationX\Core\Analytics; | |
| 7 | +use NotificationX\Core\PostType; | |
| 8 | +use NotificationX\GetInstance; | |
| 9 | +use WP_REST_Server; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * @method static BulkAction get_instance($args = null) | |
| 13 | + */ | |
| 14 | +class BulkAction { | |
| 15 | + /** | |
| 16 | + * Instance of BulkAction | |
| 17 | + * | |
| 18 | + * @var BulkAction | |
| 19 | + */ | |
| 20 | + use GetInstance; | |
| 21 | + public $namespace; | |
| 22 | + public $rest_base; | |
| 23 | + /** | |
| 24 | + * Post type. | |
| 25 | + * | |
| 26 | + * @since 4.7.0 | |
| 27 | + * @var string | |
| 28 | + */ | |
| 29 | + protected $post_type; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Constructor. | |
| 33 | + * | |
| 34 | + * @since 4.7.0 | |
| 35 | + * | |
| 36 | + * @param string $post_type Post type. | |
| 37 | + */ | |
| 38 | + public function __construct() { | |
| 39 | + $this->namespace = 'notificationx/v1'; | |
| 40 | + $this->rest_base = 'bulk-action'; | |
| 41 | + add_action( 'rest_api_init', [ $this, 'register_routes' ] ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Registers the routes for the objects of the controller. | |
| 46 | + * | |
| 47 | + * @since 4.7.0 | |
| 48 | + * | |
| 49 | + * @see register_rest_route() | |
| 50 | + */ | |
| 51 | + public function register_routes() { | |
| 52 | + register_rest_route($this->namespace, "/{$this->rest_base}/delete", | |
| 53 | + array( | |
| 54 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 55 | + 'callback' => array( $this, 'delete' ), | |
| 56 | + 'permission_callback' => [ $this, 'edit_permission' ], | |
| 57 | + 'args' => array( | |
| 58 | + 'ids' => array( | |
| 59 | + 'description' => __( 'Array of nx_id.', 'notificationx' ), | |
| 60 | + 'type' => 'array', | |
| 61 | + ), | |
| 62 | + ), | |
| 63 | + ) | |
| 64 | + ); | |
| 65 | + register_rest_route($this->namespace, "/{$this->rest_base}/regenerate", | |
| 66 | + array( | |
| 67 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 68 | + 'callback' => array( $this, 'regenerate' ), | |
| 69 | + 'permission_callback' => [ $this, 'read_permission' ], | |
| 70 | + 'args' => array( | |
| 71 | + 'ids' => array( | |
| 72 | + 'description' => __( 'Array of nx_id.', 'notificationx' ), | |
| 73 | + 'type' => 'array', | |
| 74 | + ), | |
| 75 | + ), | |
| 76 | + ) | |
| 77 | + ); | |
| 78 | + register_rest_route($this->namespace, "/{$this->rest_base}/enable", | |
| 79 | + array( | |
| 80 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 81 | + 'callback' => array( $this, 'enable' ), | |
| 82 | + 'permission_callback' => [ $this, 'edit_permission' ], | |
| 83 | + 'args' => array( | |
| 84 | + 'ids' => array( | |
| 85 | + 'description' => __( 'Array of nx_id.', 'notificationx' ), | |
| 86 | + 'type' => 'array', | |
| 87 | + ), | |
| 88 | + ), | |
| 89 | + ) | |
| 90 | + ); | |
| 91 | + register_rest_route($this->namespace, "/{$this->rest_base}/disable", | |
| 92 | + array( | |
| 93 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 94 | + 'callback' => array( $this, 'disable' ), | |
| 95 | + 'permission_callback' => [ $this, 'edit_permission' ], | |
| 96 | + 'args' => array( | |
| 97 | + 'ids' => array( | |
| 98 | + 'description' => __( 'Array of nx_id.', 'notificationx' ), | |
| 99 | + 'type' => 'array', | |
| 100 | + ), | |
| 101 | + ), | |
| 102 | + ) | |
| 103 | + ); | |
| 104 | + register_rest_route($this->namespace, "/{$this->rest_base}/reset", | |
| 105 | + array( | |
| 106 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 107 | + 'callback' => array( $this, 'reset' ), | |
| 108 | + 'permission_callback' => [ $this, 'edit_permission' ], | |
| 109 | + 'args' => array( | |
| 110 | + 'ids' => array( | |
| 111 | + 'description' => __( 'Array of nx_id.', 'notificationx' ), | |
| 112 | + 'type' => 'array', | |
| 113 | + ), | |
| 114 | + ), | |
| 115 | + ) | |
| 116 | + ); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function read_permission( $request ) { | |
| 120 | + return current_user_can( 'read_notificationx' ); | |
| 121 | + } | |
| 122 | + public function edit_permission( $request ) { | |
| 123 | + return current_user_can( 'edit_notificationx' ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + public function delete( $request ) { | |
| 127 | + $count = []; | |
| 128 | + $params = $request->get_params(); | |
| 129 | + if ( ! empty( $params['ids'] ) && is_array( $params['ids'] ) ) { | |
| 130 | + foreach ( $params['ids'] as $key => $nx_id ) { | |
| 131 | + $count[ $nx_id ] = PostType::get_instance()->delete_post( $nx_id ); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + return [ | |
| 135 | + 'success' => true, | |
| 136 | + 'count' => $count, | |
| 137 | + ]; | |
| 138 | + } | |
| 139 | + | |
| 140 | + public function regenerate( $request ) { | |
| 141 | + $count = 0; | |
| 142 | + $params = $request->get_params(); | |
| 143 | + if ( ! empty( $params['ids'] ) && is_array( $params['ids'] ) ) { | |
| 144 | + foreach ( $params['ids'] as $key => $nx_id ) { | |
| 145 | + $count += Admin::get_instance()->regenerate_notifications( [ 'nx_id' => $nx_id ] ); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + return [ | |
| 149 | + 'success' => true, | |
| 150 | + 'count' => $count, | |
| 151 | + ]; | |
| 152 | + } | |
| 153 | + | |
| 154 | + public function enable( $request ) { | |
| 155 | + $count = []; | |
| 156 | + $params = $request->get_params(); | |
| 157 | + if ( ! empty( $params['ids'] ) && is_array( $params['ids'] ) ) { | |
| 158 | + $ids = array_map( 'absint', $params['ids'] ); | |
| 159 | + $posts = PostType::get_instance()->get_posts_by_ids( $ids, null, 'nx_id, source, type' ); | |
| 160 | + if ( is_array( $posts ) ) { | |
| 161 | + foreach ( $posts as $key => $post ) { | |
| 162 | + $count[ $post['nx_id'] ] = PostType::get_instance()->update_status([ | |
| 163 | + 'nx_id' => $post['nx_id'], | |
| 164 | + 'source' => $post['source'], | |
| 165 | + 'enabled' => true, | |
| 166 | + ]); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + } | |
| 170 | + return [ | |
| 171 | + 'success' => true, | |
| 172 | + 'count' => $count, | |
| 173 | + ]; | |
| 174 | + } | |
| 175 | + | |
| 176 | + public function disable( $request ) { | |
| 177 | + $count = []; | |
| 178 | + $params = $request->get_params(); | |
| 179 | + if ( ! empty( $params['ids'] ) && is_array( $params['ids'] ) ) { | |
| 180 | + $ids = array_map( 'absint', $params['ids'] ); | |
| 181 | + $posts = PostType::get_instance()->get_posts_by_ids( $ids, null, 'nx_id, source, type' ); | |
| 182 | + if ( is_array( $posts ) ) { | |
| 183 | + foreach ( $posts as $key => $post ) { | |
| 184 | + $count[ $post['nx_id'] ] = PostType::get_instance()->update_status([ | |
| 185 | + 'nx_id' => $post['nx_id'], | |
| 186 | + 'source' => $post['source'], | |
| 187 | + 'enabled' => false, | |
| 188 | + ]); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + } | |
| 192 | + return [ | |
| 193 | + 'success' => true, | |
| 194 | + 'count' => $count, | |
| 195 | + ]; | |
| 196 | + } | |
| 197 | + | |
| 198 | + | |
| 199 | + public function reset( $request ) { | |
| 200 | + $analytics = []; | |
| 201 | + $count = 0; | |
| 202 | + $params = $request->get_params(); | |
| 203 | + if ( ! empty( $params['ids'] ) && is_array( $params['ids'] ) ) { | |
| 204 | + foreach ( $params['ids'] as $key => $nx_id ) { | |
| 205 | + Admin::get_instance()->reset_notifications( [ 'nx_id' => $nx_id ] ); | |
| 206 | + $count++; | |
| 207 | + } | |
| 208 | + $analytics = Analytics::get_instance()->get_total_count(); | |
| 209 | + } | |
| 210 | + return [ | |
| 211 | + 'success' => true, | |
| 212 | + 'count' => $count, | |
| 213 | + 'data' => $analytics, | |
| 214 | + ]; | |
| 215 | + } | |
| 216 | +} | |