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

242 lines 6.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 permission_check( WP_REST_Request $request ) {
20 $this->request = $request;
21 // $_route = $request->get_route();
22
23 // if( $_route === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) {
24 // return Helper::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->get( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] );
33 $this->get( $this->endpoint . '/themes', [ $this, 'get_themes' ] );
34 $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] );
35 $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] );
36 }
37
38 public function get_dependencies() {
39 $dependencies = Database::get_transient( $this->endpoint );
40
41 if( $dependencies ) {
42 return $this->success( $dependencies );
43 }
44
45 $response = $this->http()->query(
46 'dependencies',
47 'id, name, icon, is_pro, platforms{ id, name, file_type, icon }'
48 )->post();
49
50 if( ! is_wp_error( $response ) ) {
51 $_dependencies = [];
52 if( ! empty( $response ) ) {
53 $_dependencies[ 'unknown' ] = [];
54 foreach( $response as $dependency ) {
55 if( ! empty( $dependency['platforms'] ) ) {
56 foreach( $dependency['platforms'] as $platform ) {
57 if( ! isset( $_dependencies[ $platform['name'] ] ) ) {
58 $_dependencies[ $platform['name'] ] = [];
59 }
60 $_dependencies[ $platform['name'] ][] = $dependency;
61 }
62 } else {
63 $_dependencies[ 'unknown' ][] = $dependency;
64 }
65 }
66 }
67
68 Database::set_transient( $this->endpoint, $_dependencies );
69 return $_dependencies;
70 }
71
72 return $response;
73 }
74
75 public function check_dependencies(){
76 $dependencies = $this->get_param( 'dependencies', '', '' );
77 $platform = $this->get_param( 'platform', 'elementor' );
78
79 $_inactive_plugins = [];
80 $_plugins = Helper::get_plugins();
81
82 if( $platform === 'elementor' ) {
83 $elementor_plugin = new stdClass();
84 $elementor_plugin->name = __( 'Elementor', 'templately' );
85 $elementor_plugin->plugin_file = 'elementor/elementor.php';
86 $elementor_plugin->slug = 'elementor';
87 $elementor_plugin->is_pro = false;
88 $elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' );
89
90 $_inactive_plugins[] = $elementor_plugin;
91 }
92
93 if ( ! empty( $dependencies ) && is_array( $dependencies ) ) {
94 foreach ( $dependencies as $dependency ) {
95 if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) {
96 continue;
97 }
98
99 $dependency = ( object ) $dependency;
100 if ( is_null( $dependency->plugin_file ) ) {
101 continue;
102 }
103
104 $dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file );
105
106 if( isset( $dependency->plugin_original_slug ) ) {
107 $dependency->slug = $dependency->plugin_original_slug;
108 unset( $dependency->plugin_original_slug );
109 }
110
111 if ( $dependency->is_pro ) {
112 if ( isset( $_plugins[ $dependency->plugin_file ] ) ) {
113 unset( $dependency->is_pro );
114 $dependency->message = __( 'You have the plugin installed.', 'templately' );
115 }
116 }
117 $_inactive_plugins[] = $dependency;
118 }
119 }
120
121 return [
122 'dependencies' => $_inactive_plugins
123 ];
124 }
125
126 public function install_dependencies(){
127 $requirements = $this->get_param( 'requirement', [], '' );
128 if( empty( $requirements ) ) {
129 return $this->error(
130 'invalid_requirements',
131 __('You have supplied an invalid requirements. Please reload the page and try again.'),
132 '/install',
133 400
134 );
135 }
136 $installed = Installer::get_instance()->install( $requirements );
137 if(empty($installed['success'])){
138 return $this->error($installed['code'], $installed['message'], 'dependencies/install', 403 );
139 }
140 return $installed;
141 }
142
143 public function get_plugins() {
144 require_once ABSPATH . 'wp-admin/includes/plugin.php';
145
146 $plugins = array();
147
148 foreach ( get_plugins() as $file => $data ) {
149 // if ( is_wp_error( $this->check_read_permission( $file ) ) ) {
150 // continue;
151 // }
152
153 $data = array(
154 'plugin' => substr( $file, 0, - 4 ),
155 'status' => $this->get_plugin_status( $file ),
156 'name' => $data['Name'],
157 'plugin_uri' => $data['PluginURI'],
158 'author' => $data['Author'],
159 'author_uri' => $data['AuthorURI'],
160 'description' => array(
161 'raw' => $data['Description'],
162 'rendered' => $data['Description'],
163 ),
164 'version' => $data['Version'],
165 'network_only' => $data['Network'],
166 'requires_wp' => $data['RequiresWP'],
167 'requires_php' => $data['RequiresPHP'],
168 'textdomain' => $data['TextDomain'],
169 );
170
171 $plugins[] = $data;
172 }
173
174 return new \WP_REST_Response( $plugins );
175 }
176
177 public function get_themes() {
178 $themes = array();
179
180 $active_themes = wp_get_themes();
181 $current_theme = wp_get_theme();
182
183 foreach ( $active_themes as $theme_name => $theme ) {
184 $data = array(
185 'status' => $theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive',
186 'template' => $theme->get_template(),
187 'stylesheet' => $theme->get_stylesheet(),
188 );
189
190
191 $plain_field_mappings = array(
192 'requires_php' => 'RequiresPHP',
193 'requires_wp' => 'RequiresWP',
194 'textdomain' => 'TextDomain',
195 'version' => 'Version',
196 );
197
198 foreach ( $plain_field_mappings as $field => $header ) {
199 $data[ $field ] = $theme->get( $header );
200 }
201
202 $rich_field_mappings = array(
203 'author' => 'Author',
204 'author_uri' => 'AuthorURI',
205 'description' => 'Description',
206 'name' => 'Name',
207 'tags' => 'Tags',
208 'theme_uri' => 'ThemeURI',
209 );
210
211 foreach ( $rich_field_mappings as $field => $header ) {
212 $data[ $field ]['raw'] = $theme->display( $header, false, true );
213 $data[ $field ]['rendered'] = $theme->display( $header );
214 }
215
216 $themes[] = $data;
217 }
218
219 return new \WP_REST_Response( $themes );
220 }
221
222 /**
223 * Get's the activation status for a plugin.
224 *
225 * @since 5.5.0
226 *
227 * @param string $plugin The plugin file to check.
228 * @return string Either 'active' or 'inactive'.
229 */
230 protected function get_plugin_status( $plugin ) {
231 if ( is_plugin_active_for_network( $plugin ) ) {
232 return 'active';
233 }
234
235 if ( is_plugin_active( $plugin ) ) {
236 return 'active';
237 }
238
239 return 'inactive';
240 }
241
242 }