PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.5
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.5
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.5, at includes/API/Dependencies.php

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