Plugin Directory


Ignore:
Timestamp:
04/28/2020 02:28:16 PM (5 years ago)
Author:
tschutter
Message:

trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • codepress-admin-columns/trunk/classes/Admin/Page/Addons.php

    r2259079r2293580 
    44
    55use AC;
     6use AC\Admin;
    67use AC\Admin\Page;
    7 use AC\Message\Notice;
     8use AC\Asset\Assets;
     9use AC\Asset\Enqueueables;
     10use AC\Asset\Location;
     11use AC\Asset\Style;
    812use AC\PluginInformation;
    913
    10 class Addons extends Page
    11     implements AC\Registrable {
     14class Addons extends Page implements Enqueueables {
    1215
    1316    const NAME = 'addons';
    1417
    15     public function __construct() {
     18    /**
     19     * @var Location\Absolute
     20     */
     21    private $location;
     22
     23    /**
     24     * @var AC\Integrations
     25     */
     26    private $integrations;
     27
     28    public function __construct( Location\Absolute $location, AC\Integrations $integrations ) {
    1629        parent::__construct( self::NAME, __( 'Add-ons', 'codepress-admin-columns' ) );
    17     }
    18 
    19     /**
    20      * Register Hooks
    21      */
    22     public function register() {
    23         $this->handle_request();
    24         $this->page_notices();
    25 
    26         add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
    27     }
    28 
    29     public function page_notices() {
    30         if ( ! current_user_can( AC\Capabilities::MANAGE ) ) {
    31             return;
    32         }
    33 
    34         if ( ! ac_is_pro_active() ) {
    35             $link = ac_helper()->html->link( ac_get_site_utm_url( false, 'addon' ), __( 'Admin Columns Pro', 'codepress-admin-columns' ), array( 'target' => '_blank' ) );
    36 
    37             $this->register_notice(
    38                 sprintf( __( 'All add-ons require %s.', 'codepress-admin-columns' ), $link ),
    39                 Notice::INFO
    40             );
    41 
    42             return;
    43         }
    44 
    45         foreach ( new AC\Integrations() as $integration ) {
    46             $plugin = new PluginInformation( $integration->get_basename() );
    47 
    48             if ( ! $plugin->is_active() || $integration->is_plugin_active() ) {
    49                 continue;
    50             }
    51 
    52             $link = sprintf( '<a href="%s">%s</a>', $integration->get_plugin_link(), $integration->get_title() );
    53 
    54             $this->register_notice(
    55                 sprintf( __( '%s needs to be installed and active for the add-on to work.', 'codepress-admin-columns' ), $link ),
    56                 Notice::WARNING
    57             );
    58         }
    59     }
    60 
    61     /**
    62      * Display an activation/deactivation message on the addons page if applicable
    63      * @since 2.2
    64      */
    65     public function handle_request() {
    66         if ( ! wp_verify_nonce( filter_input( INPUT_GET, '_ac_nonce' ), 'ac-plugin-status-change' ) ) {
    67             return;
    68         }
    69 
    70         switch ( filter_input( INPUT_GET, 'status' ) ) {
    71             case 'activate' :
    72                 $this->show_activation_notice( filter_input( INPUT_GET, 'plugin' ) );
    73 
    74                 break;
    75             case 'deactivate' :
    76                 $this->show_deactivation_notice( filter_input( INPUT_GET, 'plugin' ) );
    77 
    78                 break;
    79         }
    80     }
    81 
    82     /**
    83      * @param string $slug Plugin dirname
    84      */
    85     private function show_activation_notice( $slug ) {
    86         $integration = AC\IntegrationFactory::create_by_dirname( $slug );
    87 
    88         if ( ! $integration ) {
    89             return;
    90         }
    91 
    92         $plugin = new PluginInformation( $integration->get_basename() );
    93 
    94         $plugin_name = '<strong>' . sprintf( __( '%s add-on', 'codepress-admin-columns' ), $integration->get_title() ) . '</strong>';
    95 
    96         if ( $plugin->is_active() ) {
    97             $this->register_notice(
    98                 sprintf( __( '%s successfully activated.', 'codepress-admin-columns' ), $plugin_name ),
    99                 Notice::SUCCESS
    100             );
    101 
    102             return;
    103         }
    104 
    105         $this->register_notice(
    106             sprintf( __( '%s could not be activated.', 'codepress-admin-columns' ), $plugin_name ) . ' ' . sprintf( __( 'Please visit the %s page.', 'codepress-admin-columns' ), $this->get_plugins_link() ),
    107             Notice::ERROR
    108         );
    109     }
    110 
    111     /**
    112      * @return string
    113      */
    114     private function get_plugins_link() {
    115         return ac_helper()->html->link( admin_url( 'plugins.php' ), strtolower( __( 'Plugins' ) ) );
    116     }
    117 
    118     /**
    119      * @param string $slug Plugin dirname
    120      */
    121     private function show_deactivation_notice( $slug ) {
    122         $integration = AC\IntegrationFactory::create_by_dirname( $slug );
    123 
    124         if ( ! $integration ) {
    125             return;
    126         }
    127 
    128         $this->register_notice(
    129             sprintf( __( '%s successfully deactivated.', 'codepress-admin-columns' ), '<strong>' . $integration->get_title() . '</strong>' )
    130         );
    131     }
    132 
    133     /**
    134      * Admin scripts
    135      */
    136     public function admin_scripts() {
    137         wp_enqueue_style( 'ac-admin-page-addons', AC()->get_url() . 'assets/css/admin-page-addons.css', array(), AC()->get_version() );
    138         wp_enqueue_script( 'ac-admin-page-addons', AC()->get_url() . "assets/js/admin-page-addons.js", array( 'jquery' ), AC()->get_version() );
    139 
    140         wp_localize_script( 'ac-admin-page-addons', 'AC', array(
    141                 'ajax_nonce' => wp_create_nonce( 'ac-ajax' ),
    142             )
    143         );
    144     }
    145 
    146     /**
    147      * @param string $message
    148      * @param string $type
    149      */
    150     private function register_notice( $message, $type = '' ) {
    151         $notice = new Notice( $message );
    152 
    153         if ( $type ) {
    154             $notice->set_type( $type );
    155         }
    156 
    157         $notice->register();
    158     }
    159 
    160     /**
    161      * Addons are grouped into addon groups by providing the group an addon belongs to.
    162      * @return array Available addon groups ([group_name] => [label])
    163      * @since 2.2
    164      */
    165     public function get_addon_groups() {
    166         $addon_groups = array(
    167             'installed'   => __( 'Installed', 'codepress-admin-columns' ),
    168             'recommended' => __( 'Recommended', 'codepress-admin-columns' ),
    169             'default'     => __( 'Available', 'codepress-admin-columns' ),
    170         );
    171 
    172         /**
    173          * Filter the addon groups
    174          *
    175          * @param array $addon_groups Available addon groups ([group_name] => [label])
    176          *
    177          * @since 2.2
    178          */
    179         return apply_filters( 'ac/addons/groups', $addon_groups );
    180     }
    181 
    182     /**
    183      * @param string $name
    184      *
    185      * @return string|false
    186      */
    187     public function get_group( $name ) {
    188         $groups = $this->get_addon_groups();
    189 
    190         if ( ! $groups ) {
    191             return false;
    192         }
    193 
    194         return $groups[ $name ];
    195     }
    196 
    197     /**
    198      * @param string $basename
    199      *
    200      * @return PluginInformation
    201      */
    202     private function get_plugin_info( $basename ) {
    203         return new PluginInformation( $basename );
    204     }
    205 
    206     /**
    207      * Group a list of add-ons
    208      * @return array A list of addons per group: [group_name] => (array) [group_addons], where [group_addons] is an array ([addon_name] => (array) [addon_details])
    209      * @since 3.0
    210      */
    211     private function get_grouped_addons() {
    212         $active = array();
    213         $inactive = array();
    214 
    215         foreach ( new AC\Integrations() as $integration ) {
    216             if ( $this->get_plugin_info( $integration->get_basename() )->is_active() ) {
    217                 $active[] = $integration;
    218             } else {
    219                 $inactive[] = $integration;
    220             }
    221         }
    222 
    223         /* @var AC\Integration[] $sorted */
    224         $sorted = array_merge( $active, $inactive );
    225 
    226         $grouped = array();
    227         foreach ( $this->get_addon_groups() as $group => $label ) {
    228             foreach ( $sorted as $integration ) {
    229                 $addon_group = 'default';
    230 
    231                 if ( $this->get_plugin_info( $integration->get_basename() )->is_active() ) {
    232                     $addon_group = 'recommended';
    233                 }
    234 
    235                 if ( $this->get_plugin_info( $integration->get_basename() )->is_installed() ) {
    236                     $addon_group = 'installed';
    237                 }
    238 
    239                 if ( ! isset( $grouped[ $group ] ) ) {
    240                     $grouped[ $group ]['title'] = $label;
    241                 }
    242 
    243                 if ( $addon_group === $group ) {
    244                     $grouped[ $group ]['addons'][] = $integration;
    245                 }
    246             }
    247 
    248             if ( empty( $grouped[ $group ]['addons'] ) ) {
    249                 unset( $grouped[ $group ] );
    250             }
    251         }
    252 
    253         return $grouped;
    254     }
    255 
    256     /**
    257      * Activate plugin
    258      *
    259      * @param $basename
    260      *
    261      * @return string
    262      */
    263     private function get_activation_url( $basename ) {
    264         return $this->get_plugin_action_url( 'activate', $basename );
    265     }
    266 
    267     /**
    268      * Deactivate plugin
    269      *
    270      * @param $basename
    271      *
    272      * @return string
    273      */
    274     private function get_deactivation_url( $basename ) {
    275         return $this->get_plugin_action_url( 'deactivate', $basename );
    276     }
    277 
    278     /**
    279      * Activate or Deactivate plugin
    280      *
    281      * @param string $action
    282      * @param string $basename
    283      *
    284      * @return string
    285      */
    286     private function get_plugin_action_url( $action, $basename ) {
    287         return add_query_arg( array(
    288             'action'      => $action,
    289             'plugin'      => $basename,
    290             'ac-redirect' => true,
    291         ), wp_nonce_url( admin_url( 'plugins.php' ), $action . '-plugin_' . $basename ) );
     30
     31        $this->location = $location;
     32        $this->integrations = $integrations;
     33    }
     34
     35    public function get_assets() {
     36        return new Assets( [
     37            new Style( 'ac-admin-page-addons', $this->location->with_suffix( 'assets/css/admin-page-addons.css' ) ),
     38            new Admin\Asset\Addons( 'ac-admin-page-addons', $this->location->with_suffix( 'assets/js/admin-page-addons.js' ) ),
     39        ] );
     40    }
     41
     42    public function render() {
     43        ob_start();
     44
     45        foreach ( $this->get_grouped_addons() as $group ) :
     46            ?>
     47
     48            <div class="ac-addons group-<?= esc_attr( $group['class'] ); ?>">
     49                <h2><?php echo esc_html( $group['title'] ); ?></h2>
     50
     51                <ul>
     52                    <?php
     53                    foreach ( $group['integrations'] as $addon ) {
     54                        /* @var AC\Integration $addon */
     55
     56                        $view = new AC\View( [
     57                            'logo'        => AC()->get_url() . $addon->get_logo(),
     58                            'title'       => $addon->get_title(),
     59                            'slug'        => $addon->get_slug(),
     60                            'description' => $addon->get_description(),
     61                            'actions'     => $this->render_actions( $addon ),
     62                        ] );
     63
     64                        echo $view->set_template( 'admin/edit-addon' );
     65                    }
     66                    ?>
     67                </ul>
     68            </div>
     69        <?php endforeach;
     70
     71        return ob_get_clean();
    29272    }
    29373
    โ€ฆโ€ฆ 
    30080        ob_start();
    30181
     82        $plugin = new PluginInformation( $addon->get_basename() );
     83
    30284        // Installed..
    303         if ( $this->get_plugin_info( $addon->get_basename() )->is_installed() ) :
     85        if ( $plugin->is_installed() ) :
    30486
    30587            // Active
    306             if ( $this->get_plugin_info( $addon->get_basename() )->is_active() ) : ?>
     88            if ( $plugin->is_active() ) : ?>
    30789                <span class="active"><?php _e( 'Active', 'codepress-admin-columns' ); ?></span>
    30890
    โ€ฆโ€ฆ 
    31799        // Not installed...
    318100        elseif ( ac_is_pro_active() && current_user_can( 'install_plugins' ) ) : ?>
    319 
    320101            <a href="#" class="button" data-install>
    321102                <?php esc_html_e( 'Download & Install', 'codepress-admin-columns' ); ?>
    โ€ฆโ€ฆ 
    329110
    330111    /**
    331      * @return void
    332      */
    333     public function render() {
    334         foreach ( $this->get_grouped_addons() as $group_slug => $group ) :
    335             ?>
    336 
    337             <div class="ac-addons group-<?php echo esc_attr( $group_slug ); ?>">
    338                 <h2><?php echo esc_html( $group['title'] ); ?></h2>
    339 
    340                 <ul>
    341                     <?php
    342                     foreach ( $group['addons'] as $addon ) {
    343                         /* @var AC\Integration $addon */
    344 
    345                         $view = new AC\View( array(
    346                             'logo'        => AC()->get_url() . $addon->get_logo(),
    347                             'title'       => $addon->get_title(),
    348                             'slug'        => $addon->get_slug(),
    349                             'description' => $addon->get_description(),
    350                             'actions'     => $this->render_actions( $addon ),
    351                         ) );
    352 
    353                         echo $view->set_template( 'admin/edit-addon' );
    354                     }
    355                     ?>
    356                 </ul>
    357             </div>
    358         <?php endforeach;
     112     * Deactivate plugin
     113     *
     114     * @param string $basename
     115     *
     116     * @return string
     117     */
     118    private function get_deactivation_url( $basename ) {
     119        return $this->get_plugin_action_url( 'deactivate', $basename );
     120    }
     121
     122    /**
     123     * Activate or Deactivate plugin
     124     *
     125     * @param string $action
     126     * @param string $basename
     127     *
     128     * @return string
     129     */
     130    private function get_plugin_action_url( $action, $basename ) {
     131        return add_query_arg( [
     132            'action'      => $action,
     133            'plugin'      => $basename,
     134            'ac-redirect' => true,
     135        ], wp_nonce_url( admin_url( 'plugins.php' ), $action . '-plugin_' . $basename ) );
     136    }
     137
     138    /**
     139     * Activate plugin
     140     *
     141     * @param $basename
     142     *
     143     * @return string
     144     */
     145    private function get_activation_url( $basename ) {
     146        return $this->get_plugin_action_url( 'activate', $basename );
     147    }
     148
     149    /**
     150     * @return array
     151     */
     152    private function get_grouped_addons() {
     153
     154        $active = [];
     155        $recommended = [];
     156        $available = [];
     157
     158        foreach ( $this->integrations->all() as $integration ) {
     159            $plugin = new PluginInformation( $integration->get_basename() );
     160
     161            // active
     162            if ( $plugin->is_active() ) {
     163                $active[] = $integration;
     164                continue;
     165            }
     166
     167            // recommended
     168            if ( $integration->is_plugin_active() ) {
     169                $recommended[] = $integration;
     170                continue;
     171            }
     172
     173            $available[] = $integration;
     174        }
     175
     176        $groups = [];
     177
     178        if ( $recommended ) {
     179            $groups[] = [
     180                'title'        => __( 'Recommended', 'codepress-admin-columns' ),
     181                'class'        => 'recommended',
     182                'integrations' => $recommended,
     183            ];
     184        }
     185
     186        if ( $active ) {
     187            $groups[] = [
     188                'title'        => __( 'Active', 'codepress-admin-columns' ),
     189                'class'        => 'active',
     190                'integrations' => $active,
     191            ];
     192        }
     193
     194        if ( $available ) {
     195            $groups[] = [
     196                'title'        => __( 'Available', 'codepress-admin-columns' ),
     197                'class'        => 'available',
     198                'integrations' => $available,
     199            ];
     200        }
     201
     202        return $groups;
    359203    }
    360204
Note: See TracChangeset for help on using the changeset viewer.