ajax.php
7 months ago
plugin-data-sender.php
9 months ago
plugin-installer.php
7 months ago
plugin-skin.php
7 months ago
plugin-status.php
4 years ago
utils.php
3 years ago
plugin-status.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Onboard\Classes; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | class Plugin_Status { |
| 8 | |
| 9 | private static $instance; |
| 10 | private $installedPlugins = []; |
| 11 | private $activatedPlugins = []; |
| 12 | |
| 13 | public function __construct() { |
| 14 | $this->collect_installed_plugins(); |
| 15 | $this->collect_activated_plugins(); |
| 16 | } |
| 17 | |
| 18 | private function collect_installed_plugins() { |
| 19 | foreach ( get_plugins() as $key => $plugin ) { |
| 20 | array_push( $this->installedPlugins, $key ); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | private function collect_activated_plugins() { |
| 25 | foreach ( apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) as $plugin ) { |
| 26 | array_push( $this->activatedPlugins, $plugin ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public static function instance() { |
| 31 | if ( ! static::$instance ) { |
| 32 | static::$instance = new static(); |
| 33 | } |
| 34 | |
| 35 | return static::$instance; |
| 36 | } |
| 37 | |
| 38 | public function get_installed_plugins() { |
| 39 | return $this->installedPlugins; |
| 40 | } |
| 41 | |
| 42 | public function get_activated_plugins() { |
| 43 | return $this->activatedPlugins; |
| 44 | } |
| 45 | |
| 46 | public function get_status( $name ) { |
| 47 | $data = [ |
| 48 | "url" => "", |
| 49 | "activation_url" => "", |
| 50 | "installation_url" => "", |
| 51 | "title" => "", |
| 52 | "status" => "", |
| 53 | ]; |
| 54 | |
| 55 | if ( $this->check_installed_plugin( $name ) ) { |
| 56 | if ( $this->check_activated_plugin( $name ) ) { |
| 57 | $data['title'] = __( 'Activated', 'wp-social' ); |
| 58 | $data['status'] = "activated"; |
| 59 | } else { |
| 60 | $data['title'] = __( 'Activate Now', 'wp-social' ); |
| 61 | $data['status'] = 'installed'; |
| 62 | $data['activation_url'] = $this->activation_url( $name ); |
| 63 | } |
| 64 | } else { |
| 65 | $data['title'] = __( 'Install Now', 'wp-social' ); |
| 66 | $data['status'] = 'not_installed'; |
| 67 | $data['installation_url'] = $this->installation_url( $name ); |
| 68 | $data['activation_url'] = $this->activation_url( $name ); |
| 69 | } |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | public function check_installed_plugin( $name ) { |
| 75 | return in_array( $name, $this->installedPlugins ); |
| 76 | } |
| 77 | |
| 78 | public function check_activated_plugin( $name ) { |
| 79 | return in_array( $name, $this->activatedPlugins ); |
| 80 | } |
| 81 | |
| 82 | public function activation_url( $pluginName ) { |
| 83 | |
| 84 | return wp_nonce_url( add_query_arg( |
| 85 | array( |
| 86 | 'action' => 'activate', |
| 87 | 'plugin' => $pluginName, |
| 88 | 'plugin_status' => 'all', |
| 89 | 'paged' => '1&s', |
| 90 | ), |
| 91 | admin_url( 'plugins.php' ) |
| 92 | ), 'activate-plugin_' . $pluginName ); |
| 93 | } |
| 94 | |
| 95 | public function installation_url( $pluginName ) { |
| 96 | $action = 'install-plugin'; |
| 97 | $pluginSlug = $this->get_plugin_slug( $pluginName ); |
| 98 | |
| 99 | return wp_nonce_url( |
| 100 | add_query_arg( |
| 101 | array( |
| 102 | 'action' => $action, |
| 103 | 'plugin' => $pluginSlug |
| 104 | ), |
| 105 | admin_url( 'update.php' ) |
| 106 | ), |
| 107 | $action . '_' . $pluginSlug |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function get_plugin_slug( $name ) { |
| 112 | $split = explode( '/', $name ); |
| 113 | |
| 114 | return isset( $split[0] ) ? $split[0] : null; |
| 115 | } |
| 116 | |
| 117 | public function activated_url( $pluginName ) { |
| 118 | return add_query_arg( |
| 119 | array( |
| 120 | 'page' => $this->get_plugin_slug( $pluginName ), |
| 121 | ), |
| 122 | admin_url( 'admin.php' ) ); |
| 123 | } |
| 124 | } |