| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX\Core\Rest; |
| 4 |
|
| 5 |
use NotificationX\GetInstance; |
| 6 |
use NotificationX\Admin\Admin; |
| 7 |
use WP_REST_Controller; |
| 8 |
use WP_REST_Server; |
| 9 |
|
| 10 |
/** |
| 11 |
* @method static Entries get_instance($args = null) |
| 12 |
*/ |
| 13 |
class Entries { |
| 14 |
/** |
| 15 |
* Instance of NotificationX |
| 16 |
* |
| 17 |
* @var NotificationX |
| 18 |
*/ |
| 19 |
use GetInstance; |
| 20 |
|
| 21 |
public $namespace; |
| 22 |
public $rest_base; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
* |
| 27 |
* @since 4.7.0 |
| 28 |
* |
| 29 |
* @param string $post_type Post type. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$this->namespace = 'notificationx/v1'; |
| 33 |
$this->rest_base = 'regenerate'; |
| 34 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Registers the routes for the objects of the controller. |
| 39 |
* |
| 40 |
* @since 4.7.0 |
| 41 |
* |
| 42 |
* @see register_rest_route() |
| 43 |
*/ |
| 44 |
public function register_routes() { |
| 45 |
// For entries page. |
| 46 |
// register_rest_route($namespace, '/entries/(?P<nx_id>[0-9]+)', array( |
| 47 |
// array( |
| 48 |
// 'methods' => WP_REST_Server::READABLE, |
| 49 |
// 'callback' => array($this, 'get_entries'), |
| 50 |
// 'permission_callback' => '__return_true', |
| 51 |
// 'args' => [], |
| 52 |
// ), |
| 53 |
// )); |
| 54 |
|
| 55 |
// Regenerate Notices |
| 56 |
register_rest_route($this->namespace, '/regenerate/(?P<nx_id>[0-9]+)', array( |
| 57 |
array( |
| 58 |
'methods' => WP_REST_Server::READABLE, |
| 59 |
'callback' => array($this, 'regenerate'), |
| 60 |
'permission_callback' => array($this, 'check_permission'), |
| 61 |
'args' => array( |
| 62 |
'nx_id' => array( |
| 63 |
'description' => __( 'Unique identifier for the object.', 'notificationx' ), |
| 64 |
'type' => 'integer', |
| 65 |
), |
| 66 |
), |
| 67 |
), |
| 68 |
)); |
| 69 |
|
| 70 |
// Reset Notices |
| 71 |
register_rest_route($this->namespace, '/reset/(?P<nx_id>[0-9]+)', array( |
| 72 |
array( |
| 73 |
'methods' => WP_REST_Server::READABLE, |
| 74 |
'callback' => array($this, 'reset'), |
| 75 |
'permission_callback' => array($this, 'check_permission'), |
| 76 |
'args' => array( |
| 77 |
'nx_id' => array( |
| 78 |
'description' => __( 'Unique identifier for the object.', 'notificationx' ), |
| 79 |
'type' => 'integer', |
| 80 |
), |
| 81 |
), |
| 82 |
), |
| 83 |
)); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Update one item from the collection |
| 88 |
* |
| 89 |
* @param \WP_REST_Request $request Full data about the request. |
| 90 |
* @return \WP_Error|\WP_REST_Response |
| 91 |
*/ |
| 92 |
public function regenerate($request) { |
| 93 |
$params = $request->get_params(); |
| 94 |
Admin::get_instance()->regenerate_notifications($params); |
| 95 |
wp_send_json_success(); |
| 96 |
// return new \WP_Error('cant-update', __('message', 'notificationx'), array('status' => 500)); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Reset analytics based on notification id |
| 101 |
* |
| 102 |
* @param \WP_REST_Request $request Full data about the request. |
| 103 |
* @return \WP_Error|\WP_REST_Response |
| 104 |
*/ |
| 105 |
public function reset($request) { |
| 106 |
$params = $request->get_params(); |
| 107 |
$updated_analytics = Admin::get_instance()->reset_notifications($params); |
| 108 |
wp_send_json_success($updated_analytics, 200); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function get_entries($request){ |
| 114 |
$params = $request->get_params(); |
| 115 |
$result = Entries::get_instance()->get_entries([ |
| 116 |
'nx_id' => absint( $params['nx_id'] ) |
| 117 |
]); |
| 118 |
wp_send_json($result); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if a given request has access to get items |
| 123 |
* |
| 124 |
* @param \WP_REST_Request $request Full data about the request. |
| 125 |
* @return \WP_Error|bool |
| 126 |
*/ |
| 127 |
public function check_permission( $request ) { |
| 128 |
// return current_user_can( 'edit_posts' ); |
| 129 |
return current_user_can( 'edit_notificationx' ); |
| 130 |
} |
| 131 |
} |