PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / lib / plugins-cross-sell-page / app / rest-endpoints / class-activate-plugin.php
wordpress-popup / lib / plugins-cross-sell-page / app / rest-endpoints Last commit date
class-activate-plugin.php 1 year ago class-install-plugin.php 1 year ago
class-activate-plugin.php
142 lines
1 <?php
2 /**
3 * REST API route/endpoint to activate free plugin.
4 *
5 * @link https://wpmudev.com/
6 * @since 1.0.0
7 *
8 * @author WPMUDEV (https://wpmudev.com)
9 * @package WPMUDEV\Plugin_Cross_Sell
10 *
11 * @copyright (c) 2025, WPMU DEV (http://wpmudev.com)
12 */
13
14 namespace WPMUDEV\Modules\Plugin_Cross_Sell\App\Rest_Endpoints;
15
16 use WPMUDEV\Modules\Plugin_Cross_Sell\Rest_Api;
17
18 // Abort if called directly.
19 defined( 'WPINC' ) || die;
20
21 /**
22 * Summary of Activate_Plugin
23 */
24 class Activate_Plugin extends Rest_Api {
25 /**
26 * Endpoint for activating plugin.
27 *
28 * @var string
29 */
30 protected $endpoint = '/plugincrosssell/activate_plugin';
31
32 /**
33 * Register the routes for handling confirmation functionality.
34 *
35 * @since 1.0.0
36 *
37 * @return void
38 */
39 public function register_routes(): void {
40 // Route to get auth url.
41 register_rest_route(
42 $this->get_namespace(),
43 $this->get_endpoint(),
44 array(
45 array(
46 'methods' => \WP_REST_Server::EDITABLE,
47 'callback' => array( $this, 'activate' ),
48 'permission_callback' => array( $this, 'check_permission' ),
49 'args' => $this->input_args(),
50 ),
51 )
52 );
53 }
54
55 /**
56 * Get the input arguments schema for the endpoint.
57 *
58 * @since 1.0.0
59 *
60 * @return array
61 */
62 protected function input_args(): array {
63 return array(
64 'plugin_slug' => array(
65 'type' => 'string',
66 'required' => true,
67 ),
68 'current_slug' => array(
69 'type' => 'string',
70 'required' => false,
71 ),
72 );
73 }
74
75 /**
76 * Check if the current user has permission to activate plugins.
77 *
78 * @param \WP_REST_Request $request Request object.
79 * @return bool
80 */
81 public function check_permission( \WP_REST_Request $request ): bool {
82 return $this->has_permission( $request, 'activate_plugins' );
83 }
84
85 /**
86 * Callback to activate the plugin.
87 *
88 * @param \WP_REST_Request $request Request object.
89 *
90 * @since 1.0.0
91 *
92 * @return \WP_REST_Response
93 */
94 public function activate( \WP_REST_Request $request ): \WP_REST_Response {
95 $response_message = '';
96 $success = true;
97
98 $plugin_slug = $request->get_param( 'plugin_slug' );
99 $plugin_path = $this->utilities->get_plugin_path_by_slug( $plugin_slug );
100
101 if ( empty( $plugin_path ) ) {
102 $response_message = __( 'Plugin not found.', 'plugin-cross-sell-textdomain' );
103 $success = false;
104 }
105
106 $activate = $this->activate_plugin( $plugin_path );
107
108 if ( is_wp_error( $activate ) ) {
109 $response_message = $activate->get_error_message();
110 $success = false;
111 }
112
113 $response = $this->get_response( array( 'message' => $response_message ), $success );
114
115 return $response;
116 }
117
118 /**
119 * Activate the plugin.
120 *
121 * @param string $plugin_slug Plugin slug.
122 *
123 * @since 1.0.0
124 *
125 * @return bool|\WP_Error
126 */
127 public function activate_plugin( string $plugin_slug ) {
128 if ( ! function_exists( 'activate_plugin' ) ) {
129 require_once ABSPATH . 'wp-admin/includes/plugin.php';
130 }
131
132 // Function activate_plugin returns null on success and WP_Error on failure.
133 $activate = activate_plugin( $plugin_slug );
134
135 if ( is_wp_error( $activate ) ) {
136 return $activate;
137 }
138
139 return true;
140 }
141 }
142