PluginProbe
YayMail – WooCommerce Email Customizer / trunk
YayMail – WooCommerce Email Customizer vtrunk
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / Controllers / RevisionController.php

RevisionController.php in YayMail – WooCommerce Email Customizer trunk, at src/Controllers/RevisionController.php

131 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Controllers;
4
5 use YayMail\Abstracts\BaseController;
6 use YayMail\Models\RevisionModel;
7 use YayMail\Utils\SingletonTrait;
8 use YayMail\PostTypes\TemplatePostType;
9
10 /**
11 * Revision Controller
12 *
13 * @method static RevisionController get_instance()
14 */
15 class RevisionController extends BaseController {
16 use SingletonTrait;
17
18 private $model = null;
19
20 public const YAYMAIL_TEMPLATE_REVISION_LIMIT = 50;
21
22 protected function __construct() {
23 $this->model = RevisionModel::get_instance();
24 $this->init_hooks();
25 }
26
27 protected function init_hooks() {
28 add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'set_post_has_change' ], 10, 3 );
29 add_filter( 'wp_save_post_revision', [ $this, 'filter_revision_limit' ], 10, 2 );
30
31 $revision_id_args = [
32 'revision_id' => [
33 'type' => 'number',
34 'required' => true,
35 ],
36 ];
37
38 register_rest_route(
39 YAYMAIL_REST_NAMESPACE,
40 '/revisions/query-by-template-name',
41 [
42 [
43 'methods' => \WP_REST_Server::READABLE,
44 'callback' => [ $this, 'exec_get_all_revisions' ],
45 'permission_callback' => [ $this, 'permission_callback' ],
46 ],
47 ]
48 );
49 register_rest_route(
50 YAYMAIL_REST_NAMESPACE,
51 '/revisions/delete-by-template-name',
52 [
53 [
54 'methods' => \WP_REST_Server::DELETABLE,
55 'callback' => [ $this, 'exec_delete_all_revisions' ],
56 'permission_callback' => [ $this, 'permission_callback' ],
57 ],
58 ]
59 );
60
61 register_rest_route(
62 YAYMAIL_REST_NAMESPACE,
63 '/revision/(?P<revision_id>\d+)',
64 [
65 [
66 'methods' => \WP_REST_Server::READABLE,
67 'callback' => [ $this, 'exec_get_revision_by_revision_id' ],
68 'permission_callback' => [ $this, 'permission_callback' ],
69 'args' => $revision_id_args,
70 ],
71 ]
72 );
73 }
74
75
76 /**
77 * Handle get all revisions
78 */
79 public function exec_get_all_revisions( \WP_REST_Request $request ) {
80 return $this->exec( [ $this, 'get_all_revisions' ], $request );
81 }
82 public function get_all_revisions( \WP_REST_Request $request ) {
83 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
84
85 $revisions = $this->model->get_by_template( $template_name );
86 return $revisions;
87 }
88
89 /**
90 * Handle delete all revisions
91 */
92 public function exec_delete_all_revisions( \WP_REST_Request $request ) {
93 return $this->exec( [ $this, 'delete_all_revisions' ], $request );
94 }
95 public function delete_all_revisions( \WP_REST_Request $request ) {
96 $template_name = sanitize_text_field( $request->get_param( 'template_name' ) );
97
98 $this->model->delete_by_template( $template_name );
99 return [ 'success' => true ];
100 }
101
102 /**
103 * Handle get revision by revision id
104 */
105 public function exec_get_revision_by_revision_id( \WP_REST_Request $request ) {
106 return $this->exec( [ $this, 'get_revision_by_revision_id' ], $request );
107 }
108 public function get_revision_by_revision_id( \WP_REST_Request $request ) {
109 $id = sanitize_text_field( $request->get_param( 'revision_id' ) );
110
111 return $this->model->get_by_id( $id );
112 }
113
114 /**
115 * Hooks handler
116 */
117 public function set_post_has_change( $post_has_changed, $last_revision, $post ) {
118 if ( TemplatePostType::POST_TYPE === $post->post_type ) {
119 return true;
120 }
121 return $post_has_changed;
122 }
123 public function filter_revision_limit( $limit, $post ) {
124 if ( TemplatePostType::POST_TYPE === $post->post_type ) {
125 return self::YAYMAIL_TEMPLATE_REVISION_LIMIT;
126 }
127
128 return $limit;
129 }
130 }
131