Admin
6 years ago
Ajax
6 years ago
Autoloader
6 years ago
Capabilities
6 years ago
Check
6 years ago
Column
6 years ago
Deprecated
6 years ago
Exception
6 years ago
Form
6 years ago
Helper
6 years ago
Integration
6 years ago
ListScreen
6 years ago
Message
6 years ago
Meta
6 years ago
Plugin
6 years ago
Preferences
6 years ago
Relation
6 years ago
Request
6 years ago
Response
6 years ago
Screen
6 years ago
Settings
6 years ago
Storage
6 years ago
Table
6 years ago
ThirdParty
6 years ago
Transient
6 years ago
API.php
6 years ago
Addon.php
6 years ago
Admin.php
6 years ago
AdminColumns.php
6 years ago
ArrayIterator.php
6 years ago
Autoloader.php
6 years ago
Builder.php
6 years ago
Capabilities.php
6 years ago
Collection.php
6 years ago
Column.php
6 years ago
Config.php
6 years ago
DefaultColumns.php
6 years ago
Dependencies.php
6 years ago
Expirable.php
6 years ago
Groups.php
6 years ago
Helper.php
6 years ago
Integration.php
6 years ago
IntegrationFactory.php
6 years ago
Integrations.php
6 years ago
ListScreen.php
6 years ago
ListScreenFactory.php
6 years ago
ListScreenGroups.php
6 years ago
ListScreenPost.php
6 years ago
ListScreenWP.php
6 years ago
Message.php
6 years ago
MetaType.php
6 years ago
Middleware.php
6 years ago
Plugin.php
6 years ago
PluginInformation.php
6 years ago
Preferences.php
6 years ago
Registrable.php
6 years ago
Relation.php
6 years ago
Request.php
6 years ago
Screen.php
6 years ago
ScreenController.php
6 years ago
Settings.php
6 years ago
Transient.php
6 years ago
TypedArrayIterator.php
6 years ago
View.php
6 years ago
Plugin.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC; |
| 4 | |
| 5 | use ReflectionObject; |
| 6 | use WP_Roles; |
| 7 | |
| 8 | abstract class Plugin extends Addon { |
| 9 | |
| 10 | /** @var array */ |
| 11 | private $data; |
| 12 | |
| 13 | /** |
| 14 | * Check if plugin is network activated |
| 15 | * @return bool |
| 16 | */ |
| 17 | public function is_network_active() { |
| 18 | return is_plugin_active_for_network( $this->get_basename() ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Calls get_plugin_data() for this plugin |
| 23 | * @see get_plugin_data() |
| 24 | * @return array |
| 25 | */ |
| 26 | protected function get_data() { |
| 27 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 28 | |
| 29 | if ( null === $this->data ) { |
| 30 | $this->data = get_plugin_data( $this->get_file(), false, false ); |
| 31 | } |
| 32 | |
| 33 | return $this->data; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @since 3.2 |
| 38 | * @return false|string |
| 39 | */ |
| 40 | public function get_name() { |
| 41 | return $this->get_header( 'Name' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Return a plugin header from the plugin data |
| 46 | * |
| 47 | * @param $key |
| 48 | * |
| 49 | * @return false|string |
| 50 | */ |
| 51 | protected function get_header( $key ) { |
| 52 | $data = $this->get_data(); |
| 53 | |
| 54 | if ( ! isset( $data[ $key ] ) ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | return $data[ $key ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Apply updates to the database |
| 63 | */ |
| 64 | public function install() { |
| 65 | if ( 0 === version_compare( $this->get_version(), $this->get_stored_version() ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | global $wp_roles; |
| 70 | |
| 71 | if ( ! $wp_roles ) { |
| 72 | $wp_roles = new WP_Roles(); |
| 73 | } |
| 74 | |
| 75 | do_action( 'ac/capabilities/init', $wp_roles ); |
| 76 | |
| 77 | $updater = new Plugin\Updater( $this ); |
| 78 | |
| 79 | if ( ! $updater->check_update_conditions() ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $reflection = new ReflectionObject( $this ); |
| 84 | $classes = Autoloader::instance()->get_class_names_from_dir( $reflection->getNamespaceName() . '\Plugin\Update' ); |
| 85 | |
| 86 | foreach ( $classes as $class ) { |
| 87 | $updater->add_update( new $class( $this->get_stored_version() ) ); |
| 88 | } |
| 89 | |
| 90 | $updater->parse_updates(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Check if a plugin is in beta |
| 95 | * @since 3.2 |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function is_beta() { |
| 99 | return false !== strpos( $this->get_version(), 'beta' ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return string |
| 104 | */ |
| 105 | public function get_version() { |
| 106 | return $this->get_header( 'Version' ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return string |
| 111 | */ |
| 112 | abstract protected function get_version_key(); |
| 113 | |
| 114 | /** |
| 115 | * @param string $version |
| 116 | * |
| 117 | * @return bool |
| 118 | */ |
| 119 | public function is_version_gte( $version ) { |
| 120 | return version_compare( $this->get_version(), $version, '>=' ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @return string |
| 125 | */ |
| 126 | public function get_stored_version() { |
| 127 | return get_option( $this->get_version_key() ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Update the stored version to match the (current) version |
| 132 | * |
| 133 | * @param null $version |
| 134 | * |
| 135 | * @return bool |
| 136 | */ |
| 137 | public function update_stored_version( $version = null ) { |
| 138 | if ( null === $version ) { |
| 139 | $version = $this->get_version(); |
| 140 | } |
| 141 | |
| 142 | return update_option( $this->get_version_key(), $version, false ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Check if the plugin was updated or is a new install |
| 147 | */ |
| 148 | public function is_new_install() { |
| 149 | global $wpdb; |
| 150 | |
| 151 | $sql = " |
| 152 | SELECT option_id |
| 153 | FROM {$wpdb->options} |
| 154 | WHERE option_name LIKE 'cpac_options_%' |
| 155 | LIMIT 1 |
| 156 | "; |
| 157 | |
| 158 | $results = $wpdb->get_results( $sql ); |
| 159 | |
| 160 | return empty( $results ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return a plugin header from the plugin data |
| 165 | * |
| 166 | * @param $key |
| 167 | * |
| 168 | * @deprecated |
| 169 | * @return false|string |
| 170 | */ |
| 171 | protected function get_plugin_header( $key ) { |
| 172 | _deprecated_function( __METHOD__, '3.2', 'AC\Plugin::get_header()' ); |
| 173 | |
| 174 | return $this->get_header( $key ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Calls get_plugin_data() for this plugin |
| 179 | * @deprecated |
| 180 | * @see get_plugin_data() |
| 181 | * @return array |
| 182 | */ |
| 183 | protected function get_plugin_data() { |
| 184 | _deprecated_function( __METHOD__, '3.2', 'AC\Plugin::get_data()' ); |
| 185 | |
| 186 | return $this->get_data(); |
| 187 | } |
| 188 | |
| 189 | } |