PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.4
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.4
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / API / Dependencies.php

Dependencies.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.4, at includes/API/Dependencies.php

410 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\API;
4
5 use stdClass;
6 use Templately\Utils\Helper;
7 use Templately\Utils\Database;
8 use Templately\Utils\Installer;
9 use WP_REST_Request;
10
11 use function current_user_can;
12
13
14 class Dependencies extends API {
15
16 private $endpoint = 'dependencies';
17 public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }';
18
19 public function __construct() {
20 parent::__construct();
21
22 if(!empty($_GET['disable_redirect'])) {
23 add_filter('wp_redirect', '__return_false', 999);
24 }
25 }
26
27 public function permission_check( WP_REST_Request $request ) {
28 $this->request = $request;
29
30 // Installing reaches beyond template content, so it does not ride the
31 // `delete_posts` base gate the read routes are happy with. Installer::install()
32 // checks `install_plugins` / `activate_plugins` per plugin as well; this is the
33 // front gate, so the route cannot be probed at all without the capability.
34 if ( $request->get_route() === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) {
35 return $this->error(
36 'invalid_permission',
37 __( 'Sorry, you do not have permission to install a plugin.', 'templately' ),
38 'dependencies/install',
39 rest_authorization_required_code()
40 );
41 }
42
43 return true;
44 }
45
46 public function register_routes() {
47 $this->get( $this->endpoint, [ $this, 'get_dependencies' ] );
48 $this->post( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] );
49 $this->post( $this->endpoint . '/themes', [ $this, 'get_themes' ] );
50 $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] );
51 $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] );
52 }
53
54 public function get_dependencies() {
55 $dependencies = Database::get_transient( $this->endpoint );
56
57 if( $dependencies ) {
58 return $this->success( $dependencies );
59 }
60
61 $response = $this->http()->query(
62 'dependencies',
63 'id, name, icon, is_pro, platforms{ id, name, file_type, icon }'
64 )->post();
65
66 if( ! is_wp_error( $response ) ) {
67 $_dependencies = [];
68 if( ! empty( $response ) ) {
69 $_dependencies[ 'unknown' ] = [];
70 foreach( $response as $dependency ) {
71 if( ! empty( $dependency['platforms'] ) ) {
72 foreach( $dependency['platforms'] as $platform ) {
73 if( ! isset( $_dependencies[ $platform['name'] ] ) ) {
74 $_dependencies[ $platform['name'] ] = [];
75 }
76 $_dependencies[ $platform['name'] ][] = $dependency;
77 }
78 } else {
79 $_dependencies[ 'unknown' ][] = $dependency;
80 }
81 }
82 }
83
84 Database::set_transient( $this->endpoint, $_dependencies );
85 return $_dependencies;
86 }
87
88 return $response;
89 }
90
91 public function check_dependencies(){
92 $dependencies = $this->get_param( 'dependencies', '', '' );
93 $platform = $this->get_param( 'platform', 'elementor' );
94
95 $_inactive_plugins = [];
96 $_plugins = Helper::get_plugins();
97
98 if( $platform === 'elementor' ) {
99 $elementor_plugin = new stdClass();
100 $elementor_plugin->name = __( 'Elementor', 'templately' );
101 $elementor_plugin->plugin_file = 'elementor/elementor.php';
102 $elementor_plugin->slug = 'elementor';
103 $elementor_plugin->is_pro = false;
104 $elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' );
105
106 $_inactive_plugins[] = $elementor_plugin;
107 }
108
109 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
110 foreach ( $dependencies as $dependency ) {
111 if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) {
112 continue;
113 }
114
115 $dependency = ( object ) $dependency;
116 if ( is_null( $dependency->plugin_file ) ) {
117 continue;
118 }
119
120 $dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file );
121
122 if( isset( $dependency->plugin_original_slug ) ) {
123 $dependency->slug = $dependency->plugin_original_slug;
124 unset( $dependency->plugin_original_slug );
125 }
126
127 if ( $dependency->is_pro ) {
128 if ( isset( $_plugins[ $dependency->plugin_file ] ) ) {
129 unset( $dependency->is_pro );
130 $dependency->message = __( 'You have the plugin installed.', 'templately' );
131 }
132 }
133 $_inactive_plugins[] = $dependency;
134 }
135 }
136
137 return [
138 'dependencies' => $_inactive_plugins
139 ];
140 }
141
142 public function install_dependencies(){
143 $requirements = $this->get_param( 'requirement', [], '' );
144 if( empty( $requirements ) ) {
145 return $this->error(
146 'invalid_requirements',
147 __('You have supplied an invalid requirements. Please reload the page and try again.'),
148 '/install',
149 400
150 );
151 }
152 $installed = Installer::get_instance()->install( $requirements );
153 if(empty($installed['success'])){
154 return $this->error($installed['code'], $installed['message'], 'dependencies/install', 403 );
155 }
156 return $installed;
157 }
158
159 public function get_plugins() {
160 require_once ABSPATH . 'wp-admin/includes/plugin.php';
161
162 // Get parameters from request
163 $dependencies = $this->get_param( 'dependencies', [], null );
164 $platform = $this->get_param( 'platform', 'elementor' );
165 $categories = $this->get_param( 'categories', [], null );
166
167 // Get all plugins for checking status
168 $all_plugins = array();
169 foreach ( get_plugins() as $file => $data ) {
170 $plugin_data = array(
171 'plugin' => substr( $file, 0, - 4 ),
172 'status' => $this->get_plugin_status( $file ),
173 'name' => $data['Name'],
174 'plugin_uri' => $data['PluginURI'],
175 'author' => $data['Author'],
176 'author_uri' => $data['AuthorURI'],
177 'description' => array(
178 'raw' => $data['Description'],
179 'rendered' => $data['Description'],
180 ),
181 'version' => $data['Version'],
182 'network_only' => $data['Network'],
183 'requires_wp' => $data['RequiresWP'],
184 'requires_php' => $data['RequiresPHP'],
185 'textdomain' => $data['TextDomain'],
186 );
187 $all_plugins[] = $plugin_data;
188 }
189
190 // Process dependencies and return only necessary data
191 $new_dependency_list = [];
192 $new_required_plugins = [];
193
194 // Platform-specific plugins
195 if ( $platform === 'elementor' ) {
196 $elementor_plugin = $this->find_plugin_by_name( $all_plugins, 'elementor/elementor' );
197 $e_plugin = array(
198 'plugin_file' => 'elementor/elementor.php',
199 'plugin_original_slug' => 'elementor',
200 'name' => $elementor_plugin ? $elementor_plugin['name'] : 'Elementor',
201 'installed' => $elementor_plugin ? ( $elementor_plugin['status'] === 'active' ) : false,
202 'mustHave' => true,
203 );
204 $new_dependency_list[] = $e_plugin;
205 if ( ! $elementor_plugin || $elementor_plugin['status'] !== 'active' ) {
206 $new_required_plugins[] = $e_plugin;
207 }
208 } elseif ( $platform === 'gutenberg' ) {
209 // Check if WordPress supports Gutenberg natively or if Gutenberg plugin is needed
210 $gutenberg_plugin = $this->find_plugin_by_name( $all_plugins, 'gutenberg/gutenberg' );
211
212 // Note: templately.is_wp_support_gutenberg is a frontend variable,
213 // we'll assume Gutenberg plugin is needed if it exists and is inactive
214 if ( $gutenberg_plugin && $gutenberg_plugin['status'] === 'inactive' ) {
215 $g_plugin = array(
216 'plugin_file' => 'gutenberg/gutenberg.php',
217 'plugin_original_slug' => 'gutenberg',
218 'name' => $gutenberg_plugin['name'],
219 'mustHave' => true,
220 );
221 $new_dependency_list[] = $g_plugin;
222 $new_required_plugins[] = $g_plugin;
223 }
224 }
225
226 // Process template dependencies
227 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
228 foreach ( $dependencies as $plugin_file => $plugin_obj ) {
229 if ( ! is_array( $plugin_obj ) ) {
230 continue;
231 }
232
233 $plugin_name = str_replace( '.php', '', $plugin_file );
234 $plugin = $this->find_plugin_by_name( $all_plugins, $plugin_name );
235
236 if ( $plugin && $plugin['status'] === 'active' ) {
237 $plugin_obj['installed'] = true;
238 $new_dependency_list[] = $plugin_obj;
239 } else {
240 $new_dependency_list[] = $plugin_obj;
241 $new_required_plugins[] = $plugin_obj;
242 }
243 }
244 }
245
246 // Category-based plugins
247 $included_categories = array(
248 'blog-magazine',
249 'construction',
250 'ecommerce',
251 'education',
252 'entertainment',
253 'fashion-lifestyle',
254 'features-services',
255 'food-restaurant',
256 'marketing',
257 'miscellaneous',
258 'multipurpose',
259 'nft-cryptocurrency',
260 'non-profit',
261 'technology',
262 'travel',
263 );
264
265 $is_any_category_included = false;
266 if ( ! empty( $categories ) && is_array( $categories ) ) {
267 foreach ( $categories as $category ) {
268 if ( isset( $category['slug'] ) && in_array( $category['slug'], $included_categories, true ) ) {
269 $is_any_category_included = true;
270 break;
271 }
272 }
273 }
274
275 if ( $is_any_category_included ) {
276 $nx_plugin = $this->find_plugin_by_name( $all_plugins, 'notificationx/notificationx' );
277 $notificationx = array(
278 'name' => 'NotificationX',
279 'icon' => 'https://ps.w.org/notificationx/assets/icon-256x256.gif?rev=2783824',
280 'plugin_file' => 'notificationx/notificationx.php',
281 'plugin_original_slug' => 'notificationx',
282 'is_pro' => false,
283 'installed' => $nx_plugin ? ( $nx_plugin['status'] === 'active' ) : false,
284 'link' => 'https://wordpress.org/plugins/notificationx/',
285 );
286 $new_dependency_list[] = $notificationx;
287 $new_required_plugins[] = $notificationx;
288 }
289
290 // EmbedPress for blog-magazine category
291 $ep_is_any_category_included = false;
292 if ( ! empty( $categories ) && is_array( $categories ) ) {
293 foreach ( $categories as $category ) {
294 if ( isset( $category['slug'] ) && $category['slug'] === 'blog-magazine' ) {
295 $ep_is_any_category_included = true;
296 break;
297 }
298 }
299 }
300
301 if ( $ep_is_any_category_included ) {
302 $ep_plugin = $this->find_plugin_by_name( $all_plugins, 'embedpress/embedpress' );
303 $embed_press = array(
304 'name' => 'EmbedPress',
305 'icon' => 'https://ps.w.org/embedpress/assets/icon-256x256.gif?rev=2783824',
306 'plugin_file' => 'embedpress/embedpress.php',
307 'plugin_original_slug' => 'embedpress',
308 'is_pro' => false,
309 'installed' => $ep_plugin ? ( $ep_plugin['status'] === 'active' ) : false,
310 'link' => 'https://wordpress.org/plugins/embedpress/',
311 );
312 $new_dependency_list[] = $embed_press;
313 $new_required_plugins[] = $embed_press;
314 }
315
316 return new \WP_REST_Response( array(
317 'dependencyList' => $new_dependency_list,
318 'requiredPlugins' => $new_required_plugins
319 ) );
320 }
321
322 /**
323 * Helper method to find a plugin by its name/slug
324 *
325 * @param array $plugins Array of all plugins
326 * @param string $plugin_name Plugin name to search for
327 * @return array|null Plugin data or null if not found
328 */
329 private function find_plugin_by_name( $plugins, $plugin_name ) {
330 foreach ( $plugins as $plugin ) {
331 if ( $plugin['plugin'] === $plugin_name ) {
332 return $plugin;
333 }
334 }
335 return null;
336 }
337
338 public function get_themes() {
339 // Get platform parameter from request
340 $platform = $this->get_param( 'platform', 'elementor' );
341
342 $active_themes = wp_get_themes();
343 $current_theme = wp_get_theme();
344
345 $recommended_theme = array();
346
347 if ( $platform === 'elementor' ) {
348 // Look for Hello Elementor theme
349 $target_stylesheet = 'hello-elementor';
350 $elementor_theme = null;
351
352 foreach ( $active_themes as $theme ) {
353 if ( $theme->get_stylesheet() === $target_stylesheet ) {
354 $elementor_theme = $theme;
355 break;
356 }
357 }
358
359 $recommended_theme = array(
360 'name' => $elementor_theme ? $elementor_theme->display( 'Name' ) : 'Hello Elementor',
361 'status' => $elementor_theme ? ( $elementor_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive',
362 'template' => $elementor_theme ? $elementor_theme->get_template() : 'hello-elementor',
363 'stylesheet' => $elementor_theme ? $elementor_theme->get_stylesheet() : 'hello-elementor',
364 );
365
366 } elseif ( $platform === 'gutenberg' ) {
367 // Look for Twenty Twenty-Four theme
368 $target_stylesheet = 'twentytwentyfour';
369 $gutenberg_theme = null;
370
371 foreach ( $active_themes as $theme ) {
372 if ( $theme->get_stylesheet() === $target_stylesheet ) {
373 $gutenberg_theme = $theme;
374 break;
375 }
376 }
377
378 $recommended_theme = array(
379 'name' => $gutenberg_theme ? $gutenberg_theme->display( 'Name' ) : 'Twenty Twenty-Four',
380 'status' => $gutenberg_theme ? ( $gutenberg_theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive' ) : 'inactive',
381 'template' => $gutenberg_theme ? $gutenberg_theme->get_template() : 'twentytwentyfour',
382 'stylesheet' => $gutenberg_theme ? $gutenberg_theme->get_stylesheet() : 'twentytwentyfour',
383 );
384 }
385
386 return new \WP_REST_Response( $recommended_theme );
387 }
388
389 /**
390 * Get's the activation status for a plugin.
391 *
392 * @since 5.5.0
393 *
394 * @param string $plugin The plugin file to check.
395 * @return string Either 'active' or 'inactive'.
396 */
397 protected function get_plugin_status( $plugin ) {
398 if ( is_plugin_active_for_network( $plugin ) ) {
399 return 'active';
400 }
401
402 if ( is_plugin_active( $plugin ) ) {
403 return 'active';
404 }
405
406 return 'inactive';
407 }
408
409 }
410