PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.29
Booktics – Appointment Booking Calendar for Service Businesses v1.0.29
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / module / mailpoet / Controller / mailpoet-controller.php

mailpoet-controller.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.29, at core/module/mailpoet/Controller/mailpoet-controller.php

182 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MailPoet Controller
4 *
5 * Exposes the MailPoet plugin lifecycle (install/activate) and list lookup
6 * to the admin integration card.
7 *
8 * @package Booktics/Module/Mailpoet
9 */
10
11 namespace Booktics\Module\Mailpoet\Controller;
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 use Booktics\Abstracts\Base_Rest_Controller;
16 use Arraytics\ToolsSdk\PluginManager;
17
18 class Mailpoet_Controller extends Base_Rest_Controller {
19 /**
20 * The namespace of this controller's route.
21 *
22 * @var string
23 */
24 protected $namespace = 'booktics/v1';
25
26 /**
27 * The base of this controller's route.
28 *
29 * @var string
30 */
31 protected $rest_base = 'mailpoet';
32
33 /**
34 * MailPoet plugin slug (wp.org).
35 *
36 * @var string
37 */
38 const PLUGIN_SLUG = 'mailpoet';
39
40 /**
41 * Register the routes.
42 *
43 * @return void
44 */
45 public function register_routes(): void {
46 register_rest_route(
47 $this->namespace,
48 '/' . $this->rest_base,
49 array(
50 array(
51 'methods' => \WP_REST_Server::READABLE,
52 'callback' => array( $this, 'get_status' ),
53 'permission_callback' => array( $this, 'permissions_check' ),
54 ),
55 array(
56 'methods' => \WP_REST_Server::CREATABLE,
57 'callback' => array( $this, 'update_plugin_status' ),
58 'permission_callback' => array( $this, 'permissions_check' ),
59 ),
60 )
61 );
62 }
63
64 /**
65 * Permission check.
66 *
67 * @return bool
68 */
69 public function permissions_check() {
70 return current_user_can( 'manage_options' );
71 }
72
73 /**
74 * Get current MailPoet plugin status and (when active) its lists.
75 *
76 * @return \WP_HTTP_Response
77 */
78 public function get_status() {
79 return $this->response( $this->build_status_payload() );
80 }
81
82 /**
83 * Install or activate the MailPoet plugin.
84 *
85 * @param \WP_REST_Request $request Request.
86 * @return \WP_HTTP_Response
87 */
88 public function update_plugin_status( $request ) {
89 $params = json_decode( $request->get_body(), true );
90 $status = isset( $params['status'] ) ? sanitize_text_field( $params['status'] ) : '';
91
92 if ( ! in_array( $status, array( 'install', 'activate' ), true ) ) {
93 return $this->error( __( 'Invalid status provided', 'booktics' ), 422, 'invalid_status' );
94 }
95
96 if ( 'install' === $status ) {
97 $result = PluginManager::install_plugin( self::PLUGIN_SLUG );
98 if ( $result ) {
99 PluginManager::activate_plugin( self::PLUGIN_SLUG );
100 }
101 } else {
102 $result = PluginManager::activate_plugin( self::PLUGIN_SLUG );
103 }
104
105 if ( false === $result || is_wp_error( $result ) ) {
106 return $this->error(
107 /* translators: %s: action */
108 sprintf( __( 'Could not %s MailPoet', 'booktics' ), $status ),
109 500,
110 'operation_failed'
111 );
112 }
113
114 return $this->response(
115 $this->build_status_payload(),
116 /* translators: %s: action */
117 sprintf( __( 'MailPoet %s successfully', 'booktics' ), $status )
118 );
119 }
120
121 /**
122 * Build the status payload (plugin_status + lists when active).
123 *
124 * @return array
125 */
126 private function build_status_payload() {
127 $plugin_status = $this->resolve_plugin_status();
128
129 return array(
130 'plugin_status' => $plugin_status,
131 'lists' => 'active' === $plugin_status ? $this->get_lists() : array(),
132 );
133 }
134
135 /**
136 * Resolve the plugin status: install | activate | active.
137 *
138 * @return string
139 */
140 private function resolve_plugin_status() {
141 if ( ! PluginManager::is_installed( self::PLUGIN_SLUG ) ) {
142 return 'install';
143 }
144
145 if ( ! PluginManager::is_activated( self::PLUGIN_SLUG ) ) {
146 return 'activate';
147 }
148
149 return 'active';
150 }
151
152 /**
153 * Get MailPoet lists as {id, name} pairs.
154 *
155 * @return array
156 */
157 private function get_lists() {
158 if ( ! class_exists( '\MailPoet\API\API' ) ) {
159 return array();
160 }
161
162 try {
163 $lists = \MailPoet\API\API::MP( 'v1' )->getLists();
164 } catch ( \Throwable $e ) {
165 return array();
166 }
167
168 $result = array();
169 foreach ( (array) $lists as $list ) {
170 if ( empty( $list['id'] ) ) {
171 continue;
172 }
173 $result[] = array(
174 'id' => (string) $list['id'],
175 'name' => isset( $list['name'] ) ? $list['name'] : '',
176 );
177 }
178
179 return $result;
180 }
181 }
182