column
11 years ago
storage_model
11 years ago
addons.php
11 years ago
column.php
11 years ago
settings.php
11 years ago
storage_model.php
11 years ago
third_party.php
11 years ago
upgrade.php
11 years ago
utility.php
11 years ago
addons.php
273 lines
| 1 | <?php |
| 2 | class CPAC_Addons { |
| 3 | |
| 4 | /** |
| 5 | * CPAC class |
| 6 | * |
| 7 | * @since 2.2 |
| 8 | */ |
| 9 | private $cpac; |
| 10 | |
| 11 | /** |
| 12 | * @since 2.2 |
| 13 | * |
| 14 | * @param CPAC |
| 15 | */ |
| 16 | function __construct( $cpac ) { |
| 17 | |
| 18 | $this->cpac = $cpac; |
| 19 | |
| 20 | // Redirect to addons settings tab on activation & deactivation |
| 21 | if ( is_admin() ) { |
| 22 | add_filter( 'wp_redirect', array( $this, 'addon_plugin_statuschange_redirect' ) ); |
| 23 | } |
| 24 | |
| 25 | // Handle install request |
| 26 | add_action( 'admin_init', array( $this, 'handle_install_request' ) ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Handles the installation of the add-on |
| 31 | * |
| 32 | * @since 2.2 |
| 33 | */ |
| 34 | public function handle_install_request() { |
| 35 | |
| 36 | if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'install-cac-addon' ) || ! isset( $_GET['plugin'] ) ) |
| 37 | return; |
| 38 | |
| 39 | if ( ! $this->get_addon( $_GET['plugin'] ) ) { |
| 40 | cpac_admin_message( 'Addon does not exist.', 'error' ); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if ( ! class_exists('CAC_Addon_Pro') ) { |
| 45 | cpac_admin_message( 'You need Admin Columns Pro.', 'error' ); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Hook: trigger possible warning message before running WP installer ( api errors etc. ) |
| 50 | if ( $error = apply_filters( 'cac/addons/install_request/maybe_error', false, $_GET['plugin'] ) ) { |
| 51 | cpac_admin_message( $error, 'error' ); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $install_url = add_query_arg( array( |
| 56 | 'action' => 'install-plugin', |
| 57 | 'plugin' => $_GET['plugin'], |
| 58 | 'cpac-redirect' => true |
| 59 | ), wp_nonce_url( network_admin_url( 'update.php'), 'install-plugin_' . $_GET['plugin'] ) ); |
| 60 | |
| 61 | wp_redirect( $install_url ); |
| 62 | exit; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Redirect the user to the Admin Columns add-ons page after activation/deactivation of an add-on from the add-ons page |
| 67 | * |
| 68 | * @since 2.2 |
| 69 | * |
| 70 | * @see filter:wp_redirect |
| 71 | */ |
| 72 | public function addon_plugin_statuschange_redirect( $location ) { |
| 73 | |
| 74 | if ( ! isset( $_GET['cpac-redirect'] ) ) { |
| 75 | return $location; |
| 76 | } |
| 77 | |
| 78 | $urlparts = parse_url( $location ); |
| 79 | |
| 80 | if ( ! $urlparts ) { |
| 81 | return $location; |
| 82 | } |
| 83 | |
| 84 | if ( ! empty( $urlparts['query'] ) ) { |
| 85 | $admin_url = $urlparts['scheme'] . '://' . $urlparts['host'] . $urlparts['path']; |
| 86 | |
| 87 | // activate or deactivae plugin |
| 88 | if ( admin_url( 'plugins.php' ) == $admin_url ) { |
| 89 | parse_str( $urlparts['query'], $request ); |
| 90 | |
| 91 | if ( empty( $request['error'] ) ) { |
| 92 | $location = add_query_arg( empty( $request['activate'] ) ? 'deactivate' : 'activate', true, $this->cpac->settings()->get_settings_url( 'addons' ) ); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $location; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Addons are grouped into addon groups by providing the group an addon belongs to (see CPAC_Addons::get_available_addons()). |
| 102 | * |
| 103 | * @since 2.2 |
| 104 | * |
| 105 | * @return array Available addon groups ([group_name] => [label]) |
| 106 | */ |
| 107 | public function get_addon_groups() { |
| 108 | |
| 109 | $addon_groups = array( |
| 110 | 'integration' => __( 'Third party plugin integration', 'cpac' ) |
| 111 | ); |
| 112 | |
| 113 | /** |
| 114 | * Filter the addon groups |
| 115 | * |
| 116 | * @since 2.2 |
| 117 | * |
| 118 | * @param array $addon_groups Available addon groups ([group_name] => [label]) |
| 119 | */ |
| 120 | $addon_groups = apply_filters( 'cpac/addons/addon_groups', $addon_groups ); |
| 121 | |
| 122 | return $addon_groups; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @since 2.2 |
| 127 | * |
| 128 | * @param bool $grouped Whether to group the plugins by addon group () |
| 129 | * @return array Available addons ([addon_basename] => (array) [addon_details] if not grouped, a list of these key-value pairs per group otherwise ([group_name] => (array) [group_addons])) |
| 130 | */ |
| 131 | public function get_available_addons( $grouped = false ) { |
| 132 | |
| 133 | $addons = array( |
| 134 | 'cac-addon-acf' => array( |
| 135 | 'title' => __( 'Advanced Custom Fields', 'cpac' ), |
| 136 | 'description' => __( 'Display and edit Advanced Custom Fields fields in the posts overview in seconds!', 'cpac' ), |
| 137 | 'group' => 'integration', |
| 138 | 'image' => CPAC_URL . 'assets/images/addons/acf.png' |
| 139 | ), |
| 140 | 'cac-addon-woocommerce' => array( |
| 141 | 'title' => __( 'WooCommerce', 'cpac' ), |
| 142 | 'description' => __( 'Enhance the products, orders and coupons overviews with new columns and inline editing.', 'cpac' ), |
| 143 | 'group' => 'integration', |
| 144 | 'image' => CPAC_URL . 'assets/images/addons/woocommerce.png' |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | /** |
| 149 | * Filter the available addons |
| 150 | * |
| 151 | * @since 2.2 |
| 152 | * |
| 153 | * @param array $addons Available addons ([addon_name] => (array) [addon_details]) |
| 154 | */ |
| 155 | $addons = apply_filters( 'cpac/addons/available_addons', $addons ); |
| 156 | |
| 157 | foreach ( $addons as $addon_name => $addon ) { |
| 158 | $addons[ $addon_name ] = wp_parse_args( $addon, array( |
| 159 | 'title' => '', |
| 160 | 'group' => '', |
| 161 | 'image' => '' |
| 162 | ) ); |
| 163 | } |
| 164 | |
| 165 | // Maybe group add-ons |
| 166 | if ( $grouped ) { |
| 167 | $addons = $this->group_addons( $addons ); |
| 168 | } |
| 169 | |
| 170 | return $addons; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get add-on details from the available add-ons list |
| 175 | * |
| 176 | * @since 2.2 |
| 177 | * |
| 178 | * @param string $id Unique addon ID |
| 179 | * @return bool|array Returns addon details if the add-on exists, false otherwise |
| 180 | */ |
| 181 | public function get_addon( $id ) { |
| 182 | |
| 183 | $addons = $this->get_available_addons(); |
| 184 | |
| 185 | if ( isset( $addons[ $id ] ) ) { |
| 186 | return $addons[ $id ]; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Group a list of add-ons |
| 194 | * |
| 195 | * @since 2.2 |
| 196 | * @uses CPAC_Addons::group_addons() |
| 197 | * |
| 198 | * @param array $addons List of addons ([addon_name] => (array) [addon_details]) |
| 199 | * @return array A list of addons per group: [group_name] => (array) [group_addons], where [group_addons] is an array ([addon_name] => (array) [addon_details]) |
| 200 | */ |
| 201 | public function group_addons( $addons ) { |
| 202 | |
| 203 | $groups = $this->get_addon_groups(); |
| 204 | $grouped_addons = array(); |
| 205 | |
| 206 | foreach ( $addons as $addon_name => $addon ) { |
| 207 | if ( ! isset( $groups[ $addon['group'] ] ) ) { |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if ( ! isset( $grouped_addons[ $addon['group'] ] ) ) { |
| 212 | $grouped_addons[ $addon['group'] ] = array(); |
| 213 | } |
| 214 | |
| 215 | $grouped_addons[ $addon['group'] ][ $addon_name ] = $addon; |
| 216 | } |
| 217 | return $grouped_addons; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Get whether an add-on is installed (i.e. the plugin is available in the plugin directory) |
| 222 | * |
| 223 | * @since 2.2 |
| 224 | * |
| 225 | * @param string $slug Plugin dirname/slug |
| 226 | * @return bool Returns true if there is no add-on installed with the passed ID, false otherwise |
| 227 | */ |
| 228 | public function is_addon_installed( $slug ) { |
| 229 | |
| 230 | return $this->get_installed_addon_plugin_basename( $slug ) ? true : false; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the plugin basename (see plugin_basename()) from a plugin, for example "my-plugin/my-plugin.php" |
| 235 | * |
| 236 | * @since 2.2 |
| 237 | * |
| 238 | * @param string $slug Plugin dirname/slug |
| 239 | * @return string|bool Returns the plugin basename if the plugin is installed, false otherwise |
| 240 | */ |
| 241 | public function get_installed_addon_plugin_basename( $slug ) { |
| 242 | |
| 243 | $plugins = get_plugins(); |
| 244 | |
| 245 | foreach ( $plugins as $plugin_basename => $plugin ) { |
| 246 | if ( $slug == dirname( $plugin_basename ) ) { |
| 247 | return $plugin_basename; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @since 2.2 |
| 256 | * |
| 257 | * @param string $slug Plugin dirname/slug |
| 258 | * @return string|bool Returns the plugin version if the plugin is installed, false otherwise |
| 259 | */ |
| 260 | public function get_installed_addon_plugin_version( $slug ) { |
| 261 | |
| 262 | $plugins = get_plugins(); |
| 263 | |
| 264 | foreach ( $plugins as $plugin_basename => $plugin ) { |
| 265 | if ( $slug == dirname( $plugin_basename ) ) { |
| 266 | return $plugin['Version']; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 |