PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmInstallPlugin.php

FrmInstallPlugin.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26, at classes/models/FrmInstallPlugin.php

179 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 4.04.04
8 */
9 class FrmInstallPlugin {
10
11 /**
12 * Format: folder/filename.php.
13 *
14 * @var string
15 */
16 protected $plugin_file;
17
18 /**
19 * @var string
20 */
21 protected $plugin_slug;
22
23 /**
24 * @param array $atts
25 */
26 public function __construct( $atts ) {
27 $this->plugin_file = $atts['plugin_file'];
28 list( $slug, $file ) = explode( '/', $this->plugin_file );
29 $this->plugin_slug = $slug;
30 }
31
32 /**
33 * @return string
34 */
35 public function get_activate_link() {
36 if ( $this->is_installed() && $this->is_active() ) {
37 return '';
38 }
39
40 if ( $this->is_installed() ) {
41 $url = $this->activate_url();
42 } else {
43 $url = $this->install_url();
44 }
45 return $url;
46 }
47
48 /**
49 * @return bool
50 */
51 public function is_installed() {
52 return is_dir( WP_PLUGIN_DIR . '/' . $this->plugin_slug );
53 }
54
55 /**
56 * @return bool
57 */
58 public function is_active() {
59 return is_plugin_active( $this->plugin_file );
60 }
61
62 /**
63 * @return string
64 */
65 protected function install_url() {
66 return wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $this->plugin_slug ), 'install-plugin_' . $this->plugin_slug );
67 }
68
69 /**
70 * @return string
71 */
72 protected function activate_url() {
73 return wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . $this->plugin_file ), 'activate-plugin_' . $this->plugin_file );
74 }
75
76 /**
77 * Handles the AJAX request to install a plugin.
78 *
79 * @since 6.9
80 *
81 * @return void
82 */
83 public static function ajax_install_plugin() {
84 // Check permission and nonce.
85 FrmAppHelper::permission_check( 'install_plugins' );
86 check_ajax_referer( 'frm_ajax', 'nonce' );
87
88 // Get posted data.
89 $plugin_slug = FrmAppHelper::get_post_param( 'plugin', '', 'sanitize_text_field' );
90
91 if ( ! empty( get_plugins()[ $plugin_slug ] ) ) {
92 $activate = activate_plugin( $plugin_slug );
93 } else {
94 // Include necessary files for plugin installation.
95 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
96 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
97
98 // Get the plugin information.
99 $api = plugins_api(
100 'plugin_information',
101 array(
102 'slug' => $plugin_slug,
103 'fields' => array(
104 'sections' => false,
105 ),
106 )
107 );
108
109 if ( is_wp_error( $api ) ) {
110 wp_send_json_error( $api->get_error_message() );
111 }
112
113 if ( ! FrmAddonsController::url_is_allowed( $api->versions['trunk'] ) ) {
114 wp_send_json_error( 'This download is not allowed' );
115 }
116
117 // Set up the Plugin Upgrader.
118 $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
119
120 // Install the plugin.
121 $result = $upgrader->install( $api->versions['trunk'] );
122
123 if ( is_wp_error( $result ) ) {
124 wp_send_json_error( $result->get_error_message() );
125 }
126
127 // Activate the plugin.
128 $activate = activate_plugin( $upgrader->plugin_info() );
129 }//end if
130
131 if ( is_wp_error( $activate ) ) {
132 wp_send_json_error( $activate->get_error_message() );
133 }
134
135 if ( 'wp-mail-smtp' === $plugin_slug ) {
136 update_option( 'wp_mail_smtp_activation_prevent_redirect', true );
137 }
138
139 // Send a success response.
140 wp_send_json_success( __( 'Plugin installed and activated successfully.', 'formidable' ) );
141 }
142
143 /**
144 * Checks plugin activation status via AJAX.
145 *
146 * @since 6.9
147 *
148 * @return void
149 */
150 public static function ajax_check_plugin_activation() {
151 // Check permission and nonce.
152 FrmAppHelper::permission_check( 'install_plugins' );
153 check_ajax_referer( 'frm_ajax', 'nonce' );
154
155 // Retrieve plugin identifier.
156 $plugin_path = FrmAppHelper::get_post_param( 'plugin_path', '', 'sanitize_text_field' );
157
158 // Respond based on plugin status.
159 if ( is_plugin_active( $plugin_path ) ) {
160 wp_send_json_success();
161 } else {
162 wp_send_json_error();
163 }
164 }
165
166 /**
167 * Check if a plugin is installed.
168 *
169 * @since 6.16
170 *
171 * @param string $plugin_file
172 *
173 * @return bool
174 */
175 private static function is_plugin_installed( $plugin_file ) {
176 return isset( get_plugins()[ $plugin_file ] );
177 }
178 }
179