| 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( $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 |
$elementor_plugin->is_active = Helper::is_plugin_active( 'elementor/elementor.php' ); |
| 87 |
|
| 88 |
$_inactive_plugins[] = $elementor_plugin; |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $dependencies ) && is_array( $dependencies ) ) { |
| 92 |
foreach ( $dependencies as $dependency ) { |
| 93 |
if( ! is_array( $dependency ) || ! isset( $dependency['plugin_file'] ) ) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
$dependency = ( object ) $dependency; |
| 98 |
if ( is_null( $dependency->plugin_file ) ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
$dependency->is_active = Helper::is_plugin_active( $dependency->plugin_file ); |
| 103 |
|
| 104 |
if( isset( $dependency->plugin_original_slug ) ) { |
| 105 |
$dependency->slug = $dependency->plugin_original_slug; |
| 106 |
unset( $dependency->plugin_original_slug ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( $dependency->is_pro ) { |
| 110 |
if ( isset( $_plugins[ $dependency->plugin_file ] ) ) { |
| 111 |
unset( $dependency->is_pro ); |
| 112 |
$dependency->message = __( 'You have the plugin installed.', 'templately' ); |
| 113 |
} |
| 114 |
} |
| 115 |
$_inactive_plugins[] = $dependency; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return [ |
| 120 |
'dependencies' => $_inactive_plugins |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
public function install_dependencies(){ |
| 125 |
$requirements = $this->get_param( 'requirement', [], '' ); |
| 126 |
if( empty( $requirements ) ) { |
| 127 |
return $this->error( |
| 128 |
'invalid_requirements', |
| 129 |
__('You have supplied an invalid requirements. Please reload the page and try again.'), |
| 130 |
'/install', |
| 131 |
400 |
| 132 |
); |
| 133 |
} |
| 134 |
return Installer::get_instance()->install( $requirements ); |
| 135 |
} |
| 136 |
} |