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

133 lines 3.8 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 permission_check( WP_REST_Request $request ) {
20 $this->request = $request;
21 $_route = $request->get_route();
22
23 if( $_route === '/templately/v1/dependencies/install' && ! current_user_can( 'install_plugins' ) ) {
24 return $this->error('invalid_permission', __( 'Sorry, you do not have permission to install a plugin.', 'templately' ), 'dependencies/install', 403 );
25 }
26
27 return true;
28 }
29
30 public function register_routes() {
31 $this->get( $this->endpoint, [ $this, 'get_dependencies' ] );
32 $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] );
33 $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] );
34 }
35
36 public function get_dependencies() {
37 $dependencies = Database::get_transient( $this->endpoint );
38
39 if( $dependencies ) {
40 return $this->success( $dependencies );
41 }
42
43 $response = $this->http()->query(
44 'dependencies',
45 'id, name, icon, is_pro, platforms{ id, name, file_type, icon }'
46 )->post();
47
48 if( ! is_wp_error( $response ) ) {
49 $_dependencies = [];
50 if( ! empty( $response ) ) {
51 $_dependencies[ 'unknown' ] = [];
52 foreach( $response as $dependency ) {
53 if( ! empty( $dependency['platforms'] ) ) {
54 foreach( $dependency['platforms'] as $platform ) {
55 if( ! isset( $_dependencies[ $platform['name'] ] ) ) {
56 $_dependencies[ $platform['name'] ] = [];
57 }
58 $_dependencies[ $platform['name'] ][] = $dependency;
59 }
60 } else {
61 $_dependencies[ 'unknown' ][] = $dependency;
62 }
63 }
64 }
65
66 Database::set_transient( $this->endpoint, $_dependencies );
67 return $_dependencies;
68 }
69
70 return $response;
71 }
72
73 public function check_dependencies(){
74 $dependencies = $this->get_param( 'dependencies', '', '' );
75 $platform = $this->get_param( 'platform', 'elementor' );
76
77 $_inactive_plugins = [];
78 $_plugins = Helper::get_plugins();
79
80 if( ! Helper::is_plugin_active( 'elementor/elementor.php' ) && $platform === 'elementor' ) {
81 $elementor_plugin = new stdClass();
82 $elementor_plugin->name = __( 'Elementor', 'templately' );
83 $elementor_plugin->plugin_file = 'elementor/elementor.php';
84 $elementor_plugin->slug = 'elementor';
85 $elementor_plugin->is_pro = false;
86
87 $_inactive_plugins[] = $elementor_plugin;
88 }
89
90 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
91 foreach ( $dependencies as $dependency ) {
92 if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) {
93 continue;
94 }
95
96 $dependency = ( object ) $dependency;
97 if ( is_null( $dependency->plugin_file ) || Helper::is_plugin_active( $dependency->plugin_file ) ) {
98 continue;
99 }
100
101 if( isset( $dependency->plugin_original_slug ) ) {
102 $dependency->slug = $dependency->plugin_original_slug;
103 unset( $dependency->plugin_original_slug );
104 }
105
106 if ( $dependency->is_pro ) {
107 if ( isset( $_plugins[ $dependency->plugin_file ] ) ) {
108 unset( $dependency->is_pro );
109 $dependency->message = __( 'You have the plugin installed.', 'templately' );
110 }
111 }
112 $_inactive_plugins[] = $dependency;
113 }
114 }
115
116 return [
117 'dependencies' => $_inactive_plugins
118 ];
119 }
120
121 public function install_dependencies(){
122 $requirements = $this->get_param( 'requirement', [], '' );
123 if( empty( $requirements ) ) {
124 return $this->error(
125 'invalid_requirements',
126 __('You have supplied an invalid requirements. Please reload the page and try again.'),
127 '/install',
128 400
129 );
130 }
131 return Installer::get_instance()->install( $requirements );
132 }
133 }