Addons.php
8 years ago
Columns.php
8 years ago
Help.php
8 years ago
Settings.php
8 years ago
Upgrade.php
8 years ago
Welcome.php
8 years ago
Addons.php
433 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Admin_Page_Addons extends AC_Admin_Page { |
| 8 | |
| 9 | public function __construct() { |
| 10 | $this |
| 11 | ->set_slug( 'addons' ) |
| 12 | ->set_label( __( 'Add-ons', 'codepress-admin-columns' ) ); |
| 13 | |
| 14 | add_action( 'admin_init', array( $this, 'handle_request' ) ); |
| 15 | add_filter( 'wp_redirect', array( $this, 'redirect_after_status_change' ) ); |
| 16 | add_action( 'admin_init', array( $this, 'handle_install_request' ) ); |
| 17 | add_action( 'admin_init', array( $this, 'show_missing_plugin_notice' ) ); |
| 18 | add_action( 'admin_notices', array( $this, 'missing_addon_notices' ) ); |
| 19 | add_action( 'wp_ajax_cpac_hide_install_addons_notice', array( $this, 'ajax_hide_install_addons_notice' ) ); |
| 20 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 21 | } |
| 22 | |
| 23 | public function show_missing_plugin_notice() { |
| 24 | if ( ! $this->is_current_screen() ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if ( ! current_user_can( 'manage_admin_columns' ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $addons = AC()->addons()->get_active_addons(); |
| 37 | |
| 38 | if ( ! $addons ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | foreach ( $addons as $addon ) { |
| 43 | |
| 44 | // is_plugin_installed does not work when plugins are included in a theme, that's why we check is_plugin_active |
| 45 | if ( ! $addon->is_plugin_installed() && ! $addon->is_plugin_active() ) { |
| 46 | AC()->notice( sprintf( __( '%s plugin needs to be installed for the add-on to work.', 'codepress-admin-columns' ), ac_helper()->html->link( $addon->get_plugin_url(), $addon->get_title(), array( 'target' => '_blank' ) ) ), 'notice-warning' ); |
| 47 | } else if ( ! $addon->is_plugin_active() ) { |
| 48 | $message = sprintf( __( '%s plugin is installed, but not active.', 'codepress-admin-columns' ), '<strong>' . $addon->get_plugin()->get_plugin_var( 'Name' ) . '</strong>' ); |
| 49 | |
| 50 | if ( current_user_can( 'activate_plugins' ) ) { |
| 51 | $message .= ' ' . sprintf( __( 'Click %s to activate the plugin.', 'codepress-admin-columns' ), ac_helper()->html->link( $addon->get_plugin_activation_url(), __( 'here', 'codepress-admin=n-columns' ) ) ); |
| 52 | } |
| 53 | |
| 54 | AC()->notice( $message, 'notice-warning' ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | $titles = array(); |
| 59 | foreach ( $addons as $addon ) { |
| 60 | $titles[] = '<strong>' . esc_html( $addon->get_title() ) . '</strong>'; |
| 61 | } |
| 62 | |
| 63 | if ( ! ac_is_pro_active() ) { |
| 64 | AC()->notice( sprintf( _n( '%s add-on requires %s.', '%s add-ons requires %s.', count( $titles ), 'codepress-admin-columns' ), ac_helper()->string->enumeration_list( $titles, 'and' ), ac_helper()->html->link( ac_get_site_url(), __( 'Admin Columns Pro', 'codepress-admin-columns' ), array( 'target' => '_blank' ) ) ), 'notice-warning' ); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Display an activation/deactivation message on the addons page if applicable |
| 71 | * |
| 72 | * @since 2.2 |
| 73 | */ |
| 74 | public function handle_request() { |
| 75 | if ( ! $this->is_current_screen() ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $basename = filter_input( INPUT_GET, 'plugin' ); |
| 80 | |
| 81 | if ( ! $basename ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $status = filter_input( INPUT_GET, 'status' ); |
| 86 | |
| 87 | if ( ! $status ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if ( ! wp_verify_nonce( filter_input( INPUT_GET, '_ac_nonce' ), 'ac-plugin-status-change' ) ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $plugin = new AC_PluginInformation( dirname( $basename ) ); |
| 96 | |
| 97 | $activate_string = __( '%s plugin successfully activated.', 'codepress-admin-columns' ); |
| 98 | $deactivate_string = __( '%s plugin successfully deactivated.', 'codepress-admin-columns' ); |
| 99 | |
| 100 | // Is plugin an addon? |
| 101 | foreach ( AC()->addons()->get_addons() as $addon ) { |
| 102 | if ( $addon->get_basename() === $plugin->get_basename() ) { |
| 103 | $activate_string = __( '%s successfully activated.', 'codepress-admin-columns' ); |
| 104 | $deactivate_string = __( '%s successfully deactivated.', 'codepress-admin-columns' ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | switch ( $status ) { |
| 109 | case 'activate' : |
| 110 | if ( $plugin->is_active() ) { |
| 111 | AC()->notice( sprintf( $activate_string, '<strong>' . $plugin->get_name() . '</strong>' ) ); |
| 112 | } else { |
| 113 | AC()->notice( sprintf( __( '%s could not be activated.', 'codepress-admin-columns' ), '<strong>' . $plugin->get_name() . '</strong>' ) . ' ' . sprintf( __( 'Please visit the %s page.', 'codepress-admin-columns' ), ac_helper()->html->link( admin_url( 'plugins.php' ), strtolower( __( 'Plugins' ) ) ) ), 'error' ); |
| 114 | } |
| 115 | break; |
| 116 | case 'deactivate' : |
| 117 | AC()->notice( sprintf( $deactivate_string, '<strong>' . $plugin->get_name() . '</strong>' ) ); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Admin scripts |
| 124 | */ |
| 125 | public function admin_scripts() { |
| 126 | if ( $this->is_current_screen() ) { |
| 127 | wp_enqueue_style( 'ac-admin-page-addons', AC()->get_plugin_url() . 'assets/css/admin-page-addons' . AC()->minified() . '.css', array(), AC()->get_version() ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Possibly adds an admin notice when a third party plugin supported by an addon is installed, but the addon isn't |
| 133 | * |
| 134 | * @since 2.4.9 |
| 135 | */ |
| 136 | public function missing_addon_notices() { |
| 137 | if ( ! current_user_can( 'manage_admin_columns' ) ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if ( $this->is_current_screen() ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if ( AC()->suppress_site_wide_notices() ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if ( $this->hide_notice() ) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $plugins = array(); |
| 158 | |
| 159 | foreach ( AC()->addons()->get_addons() as $addon ) { |
| 160 | if ( $addon->show_missing_notice_on_current_page() && $addon->is_plugin_active() && ! $addon->is_active() ) { |
| 161 | $plugins[] = $addon->get_title(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ( $plugins ) { |
| 166 | foreach ( $plugins as $index => $plugin ) { |
| 167 | $plugins[ $index ] = '<strong>' . $plugin . '</strong>'; |
| 168 | } |
| 169 | |
| 170 | $plugins_list = ac_helper()->string->enumeration_list( $plugins, 'and' ); |
| 171 | |
| 172 | ?> |
| 173 | <div class="ac-message updated"> |
| 174 | <a href="#" class="hide-notice hide-install-addons-notice"></a> |
| 175 | |
| 176 | <p><?php printf( __( "Did you know Admin Columns Pro has an integration addon for %s? With the proper Admin Columns Pro license, you can download them from %s!", 'codepress-admin-columns' ), $plugins_list, ac_helper()->html->link( $this->get_link(), __( 'the addons page', 'codepress-admin-columns' ) ) ); ?> |
| 177 | </div> |
| 178 | <?php |
| 179 | |
| 180 | wp_enqueue_script( 'ac-sitewide-notices' ); |
| 181 | wp_enqueue_style( 'ac-sitewide-notices' ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return bool |
| 187 | */ |
| 188 | private function hide_notice() { |
| 189 | return (bool) get_user_meta( get_current_user_id(), 'ac_hide_notice_addons', true ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Ajax callback for hiding the "Missing addons" notice used for notifying users of available integration addons for plugins they have installed |
| 194 | * |
| 195 | * @since 2.4.9 |
| 196 | */ |
| 197 | public function ajax_hide_install_addons_notice() { |
| 198 | update_user_meta( get_current_user_id(), 'ac_hide_notice_addons', true ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Handles the installation of the add-on |
| 203 | * |
| 204 | * @since 2.2 |
| 205 | */ |
| 206 | public function handle_install_request() { |
| 207 | if ( ! wp_verify_nonce( filter_input( INPUT_GET, '_wpnonce' ), 'install-ac-addon' ) ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | $addon = AC()->addons()->get_addon( filter_input( INPUT_GET, 'plugin' ) ); |
| 212 | |
| 213 | if ( ! $addon ) { |
| 214 | AC()->notice( __( 'Addon does not exist.', 'codepress-admin-columns' ), 'error' ); |
| 215 | |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | if ( ! ac_is_pro_active() ) { |
| 220 | AC()->notice( __( 'You need Admin Columns Pro.', 'codepress-admin-columns' ), 'error' ); |
| 221 | |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // Hook: trigger possible warning message before running WP installer ( api errors etc. ) |
| 226 | if ( $error = apply_filters( 'ac/addons/install_request/maybe_error', false, $_GET['plugin'] ) ) { |
| 227 | AC()->notice( $error, 'error' ); |
| 228 | |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $install_url = add_query_arg( array( |
| 233 | 'action' => 'install-plugin', |
| 234 | 'plugin' => $addon->get_slug(), |
| 235 | 'ac-redirect' => true, |
| 236 | ), wp_nonce_url( network_admin_url( 'update.php' ), 'install-plugin_' . $addon->get_slug() ) ); |
| 237 | |
| 238 | wp_redirect( $install_url ); |
| 239 | exit; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Redirect the user to the Admin Columns add-ons page after activation/deactivation of an add-on from the add-ons page |
| 244 | * |
| 245 | * @since 2.2 |
| 246 | */ |
| 247 | public function redirect_after_status_change( $location ) { |
| 248 | global $pagenow; |
| 249 | |
| 250 | if ( 'plugins.php' !== $pagenow || ! is_admin() || ! filter_input( INPUT_GET, 'ac-redirect' ) || filter_input( INPUT_GET, 'error' ) ) { |
| 251 | return $location; |
| 252 | } |
| 253 | |
| 254 | $status = filter_input( INPUT_GET, 'action' ); |
| 255 | |
| 256 | if ( ! $status ) { |
| 257 | return $location; |
| 258 | } |
| 259 | |
| 260 | $addon = false; |
| 261 | |
| 262 | // Check if either the addon is installed or it's plugin |
| 263 | foreach ( AC()->addons()->get_addons() as $_addon ) { |
| 264 | if ( in_array( filter_input( INPUT_GET, 'plugin' ), array( $_addon->get_basename(), $_addon->get_plugin_basename() ) ) ) { |
| 265 | $addon = $_addon; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if ( ! $addon ) { |
| 270 | return $location; |
| 271 | } |
| 272 | |
| 273 | $location = add_query_arg( array( |
| 274 | 'status' => $status, |
| 275 | 'plugin' => filter_input( INPUT_GET, 'plugin' ), |
| 276 | '_ac_nonce' => wp_create_nonce( 'ac-plugin-status-change' ), |
| 277 | ), $this->get_link() ); |
| 278 | |
| 279 | return $location; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Addons are grouped into addon groups by providing the group an addon belongs to. |
| 284 | * |
| 285 | * @since 2.2 |
| 286 | * |
| 287 | * @return array Available addon groups ([group_name] => [label]) |
| 288 | */ |
| 289 | public function get_addon_groups() { |
| 290 | $addon_groups = array( |
| 291 | 'installed' => __( 'Installed', 'codepress-admin-columns' ), |
| 292 | 'recommended' => __( 'Recommended', 'codepress-admin-columns' ), |
| 293 | 'default' => __( 'Available', 'codepress-admin-columns' ), |
| 294 | ); |
| 295 | |
| 296 | /** |
| 297 | * Filter the addon groups |
| 298 | * |
| 299 | * @since 2.2 |
| 300 | * |
| 301 | * @param array $addon_groups Available addon groups ([group_name] => [label]) |
| 302 | */ |
| 303 | return apply_filters( 'ac/addons/groups', $addon_groups ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @param string $name |
| 308 | * |
| 309 | * @return string|false |
| 310 | */ |
| 311 | public function get_group( $name ) { |
| 312 | $groups = $this->get_addon_groups(); |
| 313 | |
| 314 | if ( ! isset( $groups ) ) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return $groups[ $name ]; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Group a list of add-ons |
| 323 | * |
| 324 | * @since 3.0 |
| 325 | * |
| 326 | * @return array A list of addons per group: [group_name] => (array) [group_addons], where [group_addons] is an array ([addon_name] => (array) [addon_details]) |
| 327 | */ |
| 328 | private function get_grouped_addons() { |
| 329 | $active = array(); |
| 330 | $inactive = array(); |
| 331 | |
| 332 | foreach ( AC()->addons()->get_addons() as $addon ) { |
| 333 | if ( $addon->is_active() ) { |
| 334 | $active[] = $addon; |
| 335 | } else { |
| 336 | $inactive[] = $addon; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* @var AC_Addon[] $sorted */ |
| 341 | $sorted = array_merge( $active, $inactive ); |
| 342 | |
| 343 | $grouped = array(); |
| 344 | foreach ( $this->get_addon_groups() as $group => $label ) { |
| 345 | foreach ( $sorted as $addon ) { |
| 346 | $addon_group = 'default'; |
| 347 | |
| 348 | if ( $addon->is_plugin_active() ) { |
| 349 | $addon_group = 'recommended'; |
| 350 | } |
| 351 | |
| 352 | if ( $addon->is_installed() ) { |
| 353 | $addon_group = 'installed'; |
| 354 | } |
| 355 | |
| 356 | if ( ! isset( $grouped[ $group ] ) ) { |
| 357 | $grouped[ $group ]['title'] = $label; |
| 358 | } |
| 359 | |
| 360 | if ( $addon_group === $group ) { |
| 361 | $grouped[ $group ]['addons'][] = $addon; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if ( empty( $grouped[ $group ]['addons'] ) ) { |
| 366 | unset( $grouped[ $group ] ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return $grouped; |
| 371 | } |
| 372 | |
| 373 | public function display() { |
| 374 | |
| 375 | foreach ( $this->get_grouped_addons() as $group_slug => $group ) : ?> |
| 376 | <div class="ac-addon group-<?php echo esc_attr( $group_slug ); ?>"> |
| 377 | <h2><?php echo esc_html( $group['title'] ); ?></h2> |
| 378 | |
| 379 | <ul> |
| 380 | <?php |
| 381 | foreach ( $group['addons'] as $addon ) : |
| 382 | /* @var AC_Addon $addon */ ?> |
| 383 | <li class="<?php echo esc_attr( $addon->get_slug() ); ?>"> |
| 384 | <div class="addon-header"> |
| 385 | <div class="inner"> |
| 386 | <?php if ( $addon->get_logo() ) : ?> |
| 387 | <img src="<?php echo esc_attr( $addon->get_logo() ); ?>"/> |
| 388 | <?php else : ?> |
| 389 | <h2><?php echo esc_html( $addon->get_title() ); ?></h2> |
| 390 | <?php endif; ?> |
| 391 | </div> |
| 392 | </div> |
| 393 | <div class="addon-content"> |
| 394 | <h3><?php echo esc_html( $addon->get_title() ); ?></h3> |
| 395 | <p><?php echo esc_html( $addon->get_description() ); ?></p> |
| 396 | </div> |
| 397 | <div class="addon-actions"> |
| 398 | <?php |
| 399 | |
| 400 | // Installed.. |
| 401 | if ( $addon->is_installed() ) : |
| 402 | |
| 403 | // Active |
| 404 | if ( $addon->is_active() ) : ?> |
| 405 | <span class="active"><?php _e( 'Active', 'codepress-admin-columns' ); ?></span> |
| 406 | |
| 407 | <?php if ( current_user_can( 'activate_plugins' ) ) : ?> |
| 408 | <a href="<?php echo esc_url( $addon->get_deactivation_url( $addon->get_basename() ) ); ?>" class="button right"><?php _e( 'Deactivate', 'codepress-admin-columns' ); ?></a> |
| 409 | <?php endif; |
| 410 | // Installed |
| 411 | elseif ( current_user_can( 'activate_plugins' ) ) : ?> |
| 412 | <a href="<?php echo esc_url( $addon->get_activation_url( $addon->get_basename() ) ); ?>" class="button button-primary right"><?php _e( 'Activate', 'codepress-admin-columns' ); ?></a> |
| 413 | <?php endif; |
| 414 | |
| 415 | // Not installed... |
| 416 | else : |
| 417 | if ( ac_is_pro_active() && current_user_can( 'install_plugins' ) ) : ?> |
| 418 | <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'install', 'plugin' => $addon->get_slug() ), $this->get_link() ), 'install-ac-addon' ) ); ?>" class="button"><?php esc_html_e( 'Download & Install', 'codepress-admin-columns' ); ?></a> |
| 419 | <?php else : ?> |
| 420 | <a target="_blank" href="<?php echo esc_url( $addon->get_link() ); ?>" class="button"><?php esc_html_e( 'Get this add-on', 'codepress-admin-columns' ); ?></a> |
| 421 | <?php endif; |
| 422 | endif; |
| 423 | ?> |
| 424 | </div> |
| 425 | </li> |
| 426 | <?php endforeach; // addons ?> |
| 427 | </ul> |
| 428 | </div> |
| 429 | <?php endforeach; // grouped_addons |
| 430 | } |
| 431 | |
| 432 | } |
| 433 |