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