| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * This is the API for dependency checking for plugins and themes. This way plugins/themes can use this API |
|---|
| 4 | * to check to see if something is loaded or not. If its loaded and the right version |
|---|
| 5 | * passes muster as well, the API will return true. |
|---|
| 6 | * |
|---|
| 7 | * By default all plugins that are in the plugins list (active or inactive) are regiestered and |
|---|
| 8 | * this API will help plugins/themes detirmine if they can run if it meets the requirements. |
|---|
| 9 | * |
|---|
| 10 | * @package WordPress |
|---|
| 11 | * @subpackage Plugin Dependency |
|---|
| 12 | * @since 2.9 |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | class WP_Plugin_Dependency { |
|---|
| 16 | |
|---|
| 17 | var $item_array = array(); |
|---|
| 18 | |
|---|
| 19 | var $item_defaults = array('required' => true, 'oper' => '>=' ); |
|---|
| 20 | |
|---|
| 21 | // Public Functions |
|---|
| 22 | /** |
|---|
| 23 | * |
|---|
| 24 | * @return |
|---|
| 25 | * @param object $item_id |
|---|
| 26 | */ |
|---|
| 27 | function is_activated($item_id) { |
|---|
| 28 | if ($this->item_array[$item_id]['settings']['activated']) |
|---|
| 29 | return true; |
|---|
| 30 | |
|---|
| 31 | return false; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * |
|---|
| 36 | * @return |
|---|
| 37 | * @param object $item_id |
|---|
| 38 | */ |
|---|
| 39 | function activate($item_id) { |
|---|
| 40 | global $wp_version; |
|---|
| 41 | |
|---|
| 42 | if (!$this->check($item_id)) |
|---|
| 43 | return false; |
|---|
| 44 | |
|---|
| 45 | // Add "Activate" to the plugins "setting" array. |
|---|
| 46 | $item_settings = $this->item_array[$item_id]['settings']; |
|---|
| 47 | $act = array('activated' => true); |
|---|
| 48 | |
|---|
| 49 | $this->item_array[$item_id]['settings'] = array_merge($item_settings, $act); |
|---|
| 50 | |
|---|
| 51 | return true; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * |
|---|
| 56 | * @return |
|---|
| 57 | * @param object $item_id |
|---|
| 58 | */ |
|---|
| 59 | function check($item_id) { |
|---|
| 60 | global $wp_version; |
|---|
| 61 | |
|---|
| 62 | $item = $this->_get_item_data($item_id); // the name just very sanitized |
|---|
| 63 | |
|---|
| 64 | if (!$item) |
|---|
| 65 | return false; // plugin does not exist. FAIL! |
|---|
| 66 | |
|---|
| 67 | $setting = array(); |
|---|
| 68 | $wordpress = array(); |
|---|
| 69 | $item_dep = array(); |
|---|
| 70 | foreach ($item as $item_data => $data) { |
|---|
| 71 | |
|---|
| 72 | if ($item_data == 'settings') |
|---|
| 73 | $setting = $data; |
|---|
| 74 | if ($item_data == 'wordpress') |
|---|
| 75 | $wordpress = $data; |
|---|
| 76 | if ($item_data == 'plugins') |
|---|
| 77 | $item_dep = $data; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // version needed to run this plugin is passed through this function |
|---|
| 81 | // @todo: Merge WordPress Defaults in-case there are none in the plugin check. |
|---|
| 82 | if (!$this->_wordpress_version_check($wordpress['version'], $wordpress['oper'])) |
|---|
| 83 | return false; // plugin can not run since the WordPress version does not work. |
|---|
| 84 | |
|---|
| 85 | // no plugin dependinces. As long as the WordPress |
|---|
| 86 | // version is up-to-date, we can use this plugin/theme! |
|---|
| 87 | if (!is_array($item_dep)) |
|---|
| 88 | return true; |
|---|
| 89 | |
|---|
| 90 | foreach ($item_dep as $plugin => $data) { |
|---|
| 91 | |
|---|
| 92 | // @todo: Merge Plugin Defaults in-case there are none in the plugin check. |
|---|
| 93 | if ($data['required'] && !$this->_plugin_version_check($data['version'], $this->item_array[$plugin]['settings']['version'], $data['oper'])) { |
|---|
| 94 | if ($this->check($plugin)) // is it even installed? |
|---|
| 95 | return false; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // if everything passes.. tell it ok! |
|---|
| 100 | return true; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | function register() { |
|---|
| 104 | /** |
|---|
| 105 | * Major Code Upgrade |
|---|
| 106 | */ |
|---|
| 107 | $temp = array( |
|---|
| 108 | 'contact_form_7' => array ( |
|---|
| 109 | 'settings' => array ( |
|---|
| 110 | 'type' => 'plugin', |
|---|
| 111 | 'version' => '1.10.0.1' |
|---|
| 112 | ), |
|---|
| 113 | 'wordpress' => array ( |
|---|
| 114 | 'version' => '2.5', |
|---|
| 115 | 'oper' => '>=' |
|---|
| 116 | ), |
|---|
| 117 | 'plugins' => array ( |
|---|
| 118 | 'really_simple_captcha' => array ( |
|---|
| 119 | 'required' => false, |
|---|
| 120 | 'version' => '1.0', |
|---|
| 121 | 'oper' => '>=' |
|---|
| 122 | ), |
|---|
| 123 | 'really' => array ( |
|---|
| 124 | 'required' => true, |
|---|
| 125 | 'recommended' => '0.5', |
|---|
| 126 | 'version' => '0.1', |
|---|
| 127 | 'oper' => '>=' |
|---|
| 128 | ), |
|---|
| 129 | ) |
|---|
| 130 | ), |
|---|
| 131 | 'really' => array ( |
|---|
| 132 | 'settings' => array ( |
|---|
| 133 | 'type' => 'plugin', |
|---|
| 134 | 'version' => '0.3' |
|---|
| 135 | ), |
|---|
| 136 | 'wordpress' => array ( |
|---|
| 137 | 'version' => '2.8', |
|---|
| 138 | 'oper' => '>=' |
|---|
| 139 | ) |
|---|
| 140 | ), |
|---|
| 141 | ); |
|---|
| 142 | |
|---|
| 143 | foreach ($temp as $item_name => $data) { |
|---|
| 144 | $this->item_array[$item_name] = $data; |
|---|
| 145 | // tell the plugin that their checks are register'ed correctly |
|---|
| 146 | do_action('dependency_registered_' . $item_name); |
|---|
| 147 | do_action('dependency_registered', $item_name); // catch all action tigger. |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // Private Functions |
|---|
| 153 | /** |
|---|
| 154 | * |
|---|
| 155 | * Get the plugin requirments. |
|---|
| 156 | * |
|---|
| 157 | * @param string $item_id The plugin sanitized name. |
|---|
| 158 | * |
|---|
| 159 | */ |
|---|
| 160 | function _get_item_data($item_id) { |
|---|
| 161 | if (is_array($this->item_array[$item_id])) |
|---|
| 162 | return $this->item_array[$item_id]; |
|---|
| 163 | |
|---|
| 164 | return false; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * |
|---|
| 169 | * Checks to see if the item we are checking has the |
|---|
| 170 | * minuium requirments for this WordPress installation. |
|---|
| 171 | * |
|---|
| 172 | * @param string $version The version of WordPress needed for this theme/plugin to work. |
|---|
| 173 | * @param string $oper How we should compare the version numberes. |
|---|
| 174 | * @return boolen True if it's allowed, false if it doesn't pass muster. |
|---|
| 175 | * |
|---|
| 176 | */ |
|---|
| 177 | function _wordpress_version_check($version, $oper) { |
|---|
| 178 | global $wp_version; |
|---|
| 179 | |
|---|
| 180 | if (version_compare($wp_version, $version, $oper)) |
|---|
| 181 | return true; |
|---|
| 182 | else |
|---|
| 183 | return false; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * |
|---|
| 188 | * Checks to see if the item we are checking has the |
|---|
| 189 | * minuium requirments for this WordPress installation. |
|---|
| 190 | * |
|---|
| 191 | * @param string $version The version of plugins needed for this theme/plugin to work. |
|---|
| 192 | * @param string $plugin_version The version of Plugins needed for this theme/plugin to work. |
|---|
| 193 | * @return boolen True if it's allowed, false if it doesn't pass muster. |
|---|
| 194 | * |
|---|
| 195 | */ |
|---|
| 196 | function _plugin_version_check($version, $plugin_version, $oper) { |
|---|
| 197 | if (version_compare($plugin_version, $version, $oper)) |
|---|
| 198 | return true; |
|---|
| 199 | else |
|---|
| 200 | return false; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | ?> |
|---|