| @@ -15,15 +15,23 @@ | ||
| 15 | 15 | |
| 16 | 16 | private $endpoint = 'dependencies'; |
| 17 | 17 | public $query = 'dependencies{ id, name, icon, plugin_file, plugin_original_slug, is_pro, link }'; |
| 18 | 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 | + | |
| 19 | 27 | public function permission_check( WP_REST_Request $request ) { |
| 20 | 28 | $this->request = $request; |
| 21 | - $_route = $request->get_route(); | |
| 29 | + // $_route = $request->get_route(); | |
| 22 | 30 | |
| 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 | - } | |
| 31 | + // if( $_route === '/templately/v1/dependencies/install' && ! Helper::current_user_can( 'install_plugins' ) ) { | |
| 32 | + // return Helper::error('invalid_permission', __( 'Sorry, you do not have permission to install a plugin.', 'templately' ), 'dependencies/install', 403 ); | |
| 33 | + // } | |
| 26 | 34 | |
| 27 | 35 | return true; |
| 28 | 36 | } |
| 29 | 37 | |
| @@ -28,8 +36,10 @@ | ||
| 28 | 36 | } |
| 29 | 37 | |
| 30 | 38 | public function register_routes() { |
| 31 | 39 | $this->get( $this->endpoint, [ $this, 'get_dependencies' ] ); |
| 40 | + $this->get( $this->endpoint . '/plugins', [ $this, 'get_plugins' ] ); | |
| 41 | + $this->get( $this->endpoint . '/themes', [ $this, 'get_themes' ] ); | |
| 32 | 42 | $this->post( $this->endpoint . '/check', [$this, 'check_dependencies'] ); |
| 33 | 43 | $this->post( $this->endpoint . '/install', [$this, 'install_dependencies'] ); |
| 34 | 44 | } |
| 35 | 45 | |
| @@ -76,14 +86,15 @@ | ||
| 76 | 86 | |
| 77 | 87 | $_inactive_plugins = []; |
| 78 | 88 | $_plugins = Helper::get_plugins(); |
| 79 | 89 | |
| 80 | - if( ! Helper::is_plugin_active( 'elementor/elementor.php' ) && $platform === 'elementor' ) { | |
| 90 | + if( $platform === 'elementor' ) { | |
| 81 | 91 | $elementor_plugin = new stdClass(); |
| 82 | 92 | $elementor_plugin->name = __( 'Elementor', 'templately' ); |
| 83 | 93 | $elementor_plugin->plugin_file = 'elementor/elementor.php'; |
| 84 | 94 | $elementor_plugin->slug = 'elementor'; |
| 85 | 95 | $elementor_plugin->is_pro = false; |
| 96 | + $elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' ); | |
| 86 | 97 | |
| 87 | 98 | $_inactive_plugins[] = $elementor_plugin; |
| 88 | 99 | } |
| 89 | 100 | |
| @@ -93,12 +104,14 @@ | ||
| 93 | 104 | continue; |
| 94 | 105 | } |
| 95 | 106 | |
| 96 | 107 | $dependency = ( object ) $dependency; |
| 97 | - if ( is_null( $dependency->plugin_file ) || Helper::is_plugin_active( $dependency->plugin_file ) ) { | |
| 108 | + if ( is_null( $dependency->plugin_file ) ) { | |
| 98 | 109 | continue; |
| 99 | 110 | } |
| 100 | 111 | |
| 112 | + $dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file ); | |
| 113 | + | |
| 101 | 114 | if( isset( $dependency->plugin_original_slug ) ) { |
| 102 | 115 | $dependency->slug = $dependency->plugin_original_slug; |
| 103 | 116 | unset( $dependency->plugin_original_slug ); |
| 104 | 117 | } |
| @@ -127,7 +140,111 @@ | ||
| 127 | 140 | '/install', |
| 128 | 141 | 400 |
| 129 | 142 | ); |
| 130 | 143 | } |
| 131 | - return Installer::get_instance()->install( $requirements ); | |
| 144 | + $installed = Installer::get_instance()->install( $requirements ); | |
| 145 | + if(empty($installed['success'])){ | |
| 146 | + return $this->error($installed['code'], $installed['message'], 'dependencies/install', 403 ); | |
| 147 | + } | |
| 148 | + return $installed; | |
| 132 | 149 | } |
| 150 | + | |
| 151 | + public function get_plugins() { | |
| 152 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 153 | + | |
| 154 | + $plugins = array(); | |
| 155 | + | |
| 156 | + foreach ( get_plugins() as $file => $data ) { | |
| 157 | + // if ( is_wp_error( $this->check_read_permission( $file ) ) ) { | |
| 158 | + // continue; | |
| 159 | + // } | |
| 160 | + | |
| 161 | + $data = array( | |
| 162 | + 'plugin' => substr( $file, 0, - 4 ), | |
| 163 | + 'status' => $this->get_plugin_status( $file ), | |
| 164 | + 'name' => $data['Name'], | |
| 165 | + 'plugin_uri' => $data['PluginURI'], | |
| 166 | + 'author' => $data['Author'], | |
| 167 | + 'author_uri' => $data['AuthorURI'], | |
| 168 | + 'description' => array( | |
| 169 | + 'raw' => $data['Description'], | |
| 170 | + 'rendered' => $data['Description'], | |
| 171 | + ), | |
| 172 | + 'version' => $data['Version'], | |
| 173 | + 'network_only' => $data['Network'], | |
| 174 | + 'requires_wp' => $data['RequiresWP'], | |
| 175 | + 'requires_php' => $data['RequiresPHP'], | |
| 176 | + 'textdomain' => $data['TextDomain'], | |
| 177 | + ); | |
| 178 | + | |
| 179 | + $plugins[] = $data; | |
| 180 | + } | |
| 181 | + | |
| 182 | + return new \WP_REST_Response( $plugins ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + public function get_themes() { | |
| 186 | + $themes = array(); | |
| 187 | + | |
| 188 | + $active_themes = wp_get_themes(); | |
| 189 | + $current_theme = wp_get_theme(); | |
| 190 | + | |
| 191 | + foreach ( $active_themes as $theme_name => $theme ) { | |
| 192 | + $data = array( | |
| 193 | + 'status' => $theme->get_stylesheet() === $current_theme->get_stylesheet() ? 'active' : 'inactive', | |
| 194 | + 'template' => $theme->get_template(), | |
| 195 | + 'stylesheet' => $theme->get_stylesheet(), | |
| 196 | + ); | |
| 197 | + | |
| 198 | + | |
| 199 | + $plain_field_mappings = array( | |
| 200 | + 'requires_php' => 'RequiresPHP', | |
| 201 | + 'requires_wp' => 'RequiresWP', | |
| 202 | + 'textdomain' => 'TextDomain', | |
| 203 | + 'version' => 'Version', | |
| 204 | + ); | |
| 205 | + | |
| 206 | + foreach ( $plain_field_mappings as $field => $header ) { | |
| 207 | + $data[ $field ] = $theme->get( $header ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + $rich_field_mappings = array( | |
| 211 | + 'author' => 'Author', | |
| 212 | + 'author_uri' => 'AuthorURI', | |
| 213 | + 'description' => 'Description', | |
| 214 | + 'name' => 'Name', | |
| 215 | + 'tags' => 'Tags', | |
| 216 | + 'theme_uri' => 'ThemeURI', | |
| 217 | + ); | |
| 218 | + | |
| 219 | + foreach ( $rich_field_mappings as $field => $header ) { | |
| 220 | + $data[ $field ]['raw'] = $theme->display( $header, false, true ); | |
| 221 | + $data[ $field ]['rendered'] = $theme->display( $header ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + $themes[] = $data; | |
| 225 | + } | |
| 226 | + | |
| 227 | + return new \WP_REST_Response( $themes ); | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Get's the activation status for a plugin. | |
| 232 | + * | |
| 233 | + * @since 5.5.0 | |
| 234 | + * | |
| 235 | + * @param string $plugin The plugin file to check. | |
| 236 | + * @return string Either 'active' or 'inactive'. | |
| 237 | + */ | |
| 238 | + protected function get_plugin_status( $plugin ) { | |
| 239 | + if ( is_plugin_active_for_network( $plugin ) ) { | |
| 240 | + return 'active'; | |
| 241 | + } | |
| 242 | + | |
| 243 | + if ( is_plugin_active( $plugin ) ) { | |
| 244 | + return 'active'; | |
| 245 | + } | |
| 246 | + | |
| 247 | + return 'inactive'; | |
| 248 | + } | |
| 249 | + | |
| 133 | 250 | } |