Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PluginsController | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| get_plugin_queue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get_plugins_status | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
306 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\ECommerce\RestApi; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Installer\Data\Options as InstallerOptions; |
| 6 | use NewfoldLabs\WP\Module\Installer\TaskManagers\PluginInstallTaskManager; |
| 7 | use NewfoldLabs\WP\Module\ECommerce\Permissions; |
| 8 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 9 | use NewfoldLabs\WP\Module\ECommerce\Data\Plugins; |
| 10 | |
| 11 | /** |
| 12 | * Class PluginsController |
| 13 | */ |
| 14 | class PluginsController { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * REST namespace |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $namespace = 'newfold-ecommerce/v1'; |
| 23 | |
| 24 | /** |
| 25 | * REST base |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $rest_base = '/plugins'; |
| 30 | |
| 31 | /** |
| 32 | * Container loaded from the brand plugin. |
| 33 | * |
| 34 | * @var Container |
| 35 | */ |
| 36 | protected $container; |
| 37 | |
| 38 | public function __construct( Container $container ) { |
| 39 | $this->container = $container; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Registers rest routes for PluginsController class. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function register_routes() { |
| 48 | \register_rest_route( |
| 49 | $this->namespace, |
| 50 | $this->rest_base . '/status', |
| 51 | array( |
| 52 | array( |
| 53 | 'methods' => \WP_REST_Server::READABLE, |
| 54 | 'callback' => array( $this, 'get_plugins_status' ), |
| 55 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 56 | ), |
| 57 | ) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | private function get_plugin_queue() { |
| 62 | $queue_name = PluginInstallTaskManager::get_queue_name(); |
| 63 | return \get_option( InstallerOptions::get_option_name( $queue_name ), array()); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get status of supported plugins. |
| 68 | * |
| 69 | * @return \WP_REST_Response |
| 70 | */ |
| 71 | public function get_plugins_status( \WP_REST_Request $request) { |
| 72 | $pluginFilter = $request->get_param('plugins'); |
| 73 | $requestedPlugins = array( ); |
| 74 | if ( isset( $pluginFilter ) ) { |
| 75 | $requestedPlugins = explode(',', $pluginFilter); |
| 76 | } |
| 77 | $details = array(); |
| 78 | foreach ( Plugins::supported_plugins() as $plugin => $info ) { |
| 79 | if ( !in_array($plugin, $requestedPlugins) && !in_array('all', $requestedPlugins) ) { |
| 80 | continue; |
| 81 | } |
| 82 | $status = 'need_to_install'; |
| 83 | if ( |
| 84 | (isset($info['file_extended']) && file_exists(WP_PLUGIN_DIR . '/' . $info['file_extended'])) || |
| 85 | (isset($info['file_premium']) && file_exists(WP_PLUGIN_DIR . '/' . $info['file_premium'])) || |
| 86 | file_exists(WP_PLUGIN_DIR . '/' . $info['file']) |
| 87 | ){ |
| 88 | $active = ( |
| 89 | (isset($info['file_extended']) && \is_plugin_active($info['file_extended'])) || |
| 90 | (isset($info['file_premium']) && \is_plugin_active($info['file_premium'])) || |
| 91 | \is_plugin_active($info['file']) |
| 92 | ); |
| 93 | if ( $active ) { |
| 94 | $status = 'active'; |
| 95 | } else { |
| 96 | $status = 'need_to_activate'; |
| 97 | } |
| 98 | } |
| 99 | $details[ $plugin ] = array( |
| 100 | 'status' => $status, |
| 101 | 'url' => \admin_url( $info['url'] ), |
| 102 | ); |
| 103 | } |
| 104 | $plugins_queue = array_column( $this->get_plugin_queue(), 'slug' ); |
| 105 | $plugin_being_installed = \get_option( InstallerOptions::get_option_name( 'plugins_init_status' ), false); |
| 106 | $plugin_being_installed_queue = \get_option( InstallerOptions::get_option_name( 'plugin_install_queue' ), array()); |
| 107 | if ($plugin_being_installed !== false && $plugin_being_installed !== 'completed') { |
| 108 | $plugins_queue[] = $plugin_being_installed_queue; |
| 109 | } |
| 110 | return new \WP_REST_Response( |
| 111 | array( |
| 112 | 'details' => $details, |
| 113 | 'queue' => $plugins_queue, |
| 114 | ), |
| 115 | 200 |
| 116 | ); |
| 117 | } |
| 118 | } |