Admin
8 years ago
Column
8 years ago
Form
8 years ago
Helper
8 years ago
ListScreen
8 years ago
Meta
8 years ago
Notice
8 years ago
Plugin
8 years ago
Relation
8 years ago
Settings
8 years ago
ThirdParty
8 years ago
API.php
8 years ago
Addon.php
8 years ago
Admin.php
8 years ago
Autoloader.php
8 years ago
Collection.php
8 years ago
Column.php
8 years ago
Groups.php
8 years ago
Helper.php
8 years ago
ListScreen.php
8 years ago
ListScreenPost.php
8 years ago
ListScreenWP.php
8 years ago
Plugin.php
8 years ago
PluginInformation.php
8 years ago
Preferences.php
8 years ago
Relation.php
8 years ago
TableScreen.php
8 years ago
View.php
8 years ago
ViewInterface.php
8 years ago
Plugin.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | abstract class AC_Plugin extends AC_Addon { |
| 8 | |
| 9 | /** |
| 10 | * Check if plugin is network activated |
| 11 | * |
| 12 | * @return bool |
| 13 | */ |
| 14 | public function is_network_active() { |
| 15 | return is_plugin_active_for_network( $this->get_basename() ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Calls get_plugin_data() for this plugin |
| 20 | * |
| 21 | * @see get_plugin_data() |
| 22 | * @return array |
| 23 | */ |
| 24 | protected function get_plugin_data() { |
| 25 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 26 | |
| 27 | return get_plugin_data( $this->get_file(), false, false ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Return a plugin header from the plugin data |
| 32 | * |
| 33 | * @param $key |
| 34 | * |
| 35 | * @return false|string |
| 36 | */ |
| 37 | protected function get_plugin_header( $key ) { |
| 38 | $data = $this->get_plugin_data(); |
| 39 | |
| 40 | if ( ! isset( $data[ $key ] ) ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return $data[ $key ]; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return the prefix that is used by this plugin |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | abstract public function get_prefix(); |
| 53 | |
| 54 | /** |
| 55 | * Apply updates to the database |
| 56 | * |
| 57 | * @param null|string $updates_dir |
| 58 | */ |
| 59 | public function install() { |
| 60 | if ( 0 === version_compare( $this->get_version(), $this->get_stored_version() ) ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $updater = new AC_Plugin_Updater( $this ); |
| 65 | |
| 66 | if ( ! $updater->check_update_conditions() ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $classes = AC()->autoloader()->get_class_names_from_dir( $this->get_plugin_dir() . 'classes/Plugin/Update', $this->get_prefix() ); |
| 71 | |
| 72 | foreach ( $classes as $class ) { |
| 73 | $updater->add_update( new $class( $this->get_stored_version() ) ); |
| 74 | } |
| 75 | |
| 76 | $updater->parse_updates(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return string |
| 81 | */ |
| 82 | public function get_version() { |
| 83 | return $this->get_plugin_header( 'Version' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return string |
| 88 | */ |
| 89 | abstract protected function get_version_key(); |
| 90 | |
| 91 | /** |
| 92 | * @return string |
| 93 | */ |
| 94 | public function get_stored_version() { |
| 95 | return get_option( $this->get_version_key() ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Update the stored version to match the (current) version |
| 100 | */ |
| 101 | public function update_stored_version( $version = null ) { |
| 102 | if ( null === $version ) { |
| 103 | $version = $this->get_version(); |
| 104 | } |
| 105 | |
| 106 | return update_option( $this->get_version_key(), $version ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Check if the plugin was updated or is a new install |
| 111 | */ |
| 112 | public function is_new_install() { |
| 113 | global $wpdb; |
| 114 | |
| 115 | $sql = " |
| 116 | SELECT option_id |
| 117 | FROM {$wpdb->options} |
| 118 | WHERE option_name LIKE 'cpac_options_%' |
| 119 | LIMIT 1 |
| 120 | "; |
| 121 | |
| 122 | $results = $wpdb->get_results( $sql ); |
| 123 | |
| 124 | return empty( $results ); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 |