Plugin Directory

Changeset 1797093


Ignore:
Timestamp:
01/04/2018 03:49:48 PM (8 years ago)
Author:
ClearcodeHQ
Message:

Version 1.1.0

Location:
cc-auto-activate-plugins
Files:
13 added
4 edited

Legend:

Unmodified
Added
Removed
  • cc-auto-activate-plugins/trunk/README.txt

    r1796330 r1797093  
    1414
    1515This plugin automatically activate all Plugins from WP_PLUGIN_DIR.
     16
     17= Tips & Tricks =
     18
     191. You can use plugin as Must Use Plugin (you can also use [CC-MU-Plugins-Loader](https://wordpress.org/plugins/cc-mu-plugins-loader) to load it).
     202. You can exclude plugins from auto activate by using `Clearcode\Auto_Activate_Plugins` filter.
     21
     22`add_filter( 'Clearcode\Auto_Activate_Plugins', function( $plugins ) {
     23    foreach ( [
     24        'example/plugin.php'
     25    ] as $plugin ) if ( isset( $plugins[ $plugin ] ) ) unset( $plugins[ $plugin ] );
     26    return $plugins;
     27} );`
    1628
    1729== Installation ==
     
    3547== Changelog ==
    3648
     49= 1.1.0 =
     50*Release date: 04.01.2018*
     51
     52* Added support for WordPress core 'activate_plugin' function.
     53
    3754= 1.0.0 =
    3855*Release date: 02.01.2018*
  • cc-auto-activate-plugins/trunk/includes/class-auto-activate-plugins.php

    r1796330 r1797093  
    2929if ( ! class_exists( __NAMESPACE__ . '\Auto_Activate_Plugins' ) ) {
    3030    class Auto_Activate_Plugins extends Plugin {
    31         protected $plugins = array();
     31        protected $plugins = [];
    3232
    3333        public function __construct() {
    3434            parent::__construct();
    3535
    36             if ( ! get_option( self::get( 'slug' ) ) ) {
    37                 add_option( self::get( 'slug' ), array() );
    38             }
    39             $this->plugins = get_option( self::get( 'slug' ) );
     36            $this->plugins = get_plugins();
     37        }
     38
     39        protected function get_plugins() {
     40            $plugins = $this->plugins;
     41            $plugin  = plugin_basename( self::get( 'file' ) );
     42            if ( isset( $plugins[ $plugin ] ) ) unset( $plugins[ $plugin ] );
     43            return apply_filters( self::get( 'slug' ), $plugins );
    4044        }
    4145
    4246        public function action_after_setup_theme() {
    43             $activate_plugins = apply_filters( self::get( 'slug' ), array_map( '__return_zero', get_plugins() ) );
    44 
    45             if ( $activate_plugins !== $this->plugins ) {
    46                 $this->plugins = $activate_plugins;
    47                 foreach ( $activate_plugins as $plugin => $network_wide ) {
    48                     if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
    49                         if ( $network_wide ) {
    50                             $current            = get_site_option( 'active_sitewide_plugins', array() );
    51                             $current[ $plugin ] = time();
    52                             update_site_option( 'active_sitewide_plugins', $current );
    53                         } else {
    54                             $current   = get_option( 'active_plugins', array() );
    55                             $current[] = $plugin;
    56                             sort( $current );
    57                             update_option( 'active_plugins', $current );
    58                         }
    59                     } else {
    60                         unset( $this->plugins[ $plugin ] );
    61                     }
    62                 }
    63                 update_option( self::get( 'slug' ), $this->plugins );
    64             }
     47            foreach( $this->get_plugins() as $plugin => $data )
     48                if ( in_array( $plugin, array_keys( $this->plugins ) ) )
     49                    if ( is_wp_error( $result = activate_plugin( $plugin, ! empty( $data['Network'] ) ) ) )
     50                        error_log( self::get( 'slug' ) . ': ' . $result->get_error_message() );
    6551        }
    6652
    67         public function filter_pre_update_option_active_plugins_100( $value, $old_value ) {
    68             $new_value = array();
    69             if ( ! empty( $this->plugins ) ) {
    70                 foreach ( $this->plugins as $plugin => $network_wide ) {
    71                     $new_value[] = $plugin;
    72                     if ( in_array( $plugin, $value ) ) {
    73                         if ( ( $key = array_search( $plugin, $value ) ) !== false ) {
    74                             unset( $value[ $key ] );
    75                         }
    76                     }
    77                 }
    78             }
    79             if ( ! empty( $new_value ) && ! empty( $value ) ) {
    80                 $new_value = array_merge( $new_value, $value );
    81             } else if ( ! empty( $value ) ) {
    82                 $new_value = $value;
    83             }
    84 
    85             return $new_value;
    86         }
    87 
    88         public function filter_pre_update_site_option_active_sitewide_plugins_100( $value, $old_value ) {
    89             $new_value = array();
    90             if ( ! empty( $this->plugins ) ) {
    91                 foreach ( $this->plugins as $plugin => $network_wide ) {
    92                     if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin ) && $network_wide ) {
    93                         $new_value[ $plugin ] = time();
    94                         if ( array_key_exists( $plugin, $value ) ) {
    95                             $new_value[ $plugin ] = $value[ $plugin ];
    96                             unset( $value[ $plugin ] );
    97                         }
    98                     }
    99                 }
    100             }
    101             if ( ! empty( $new_value ) && ! empty( $value ) ) {
    102                 $new_value = array_merge( $new_value, $value );
    103             } else if ( ! empty( $value ) ) {
    104                 $new_value = $value;
    105             }
    106 
    107             return $new_value;
    108         }
    109 
    110         public function filter_network_admin_plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
    111             if ( array_key_exists( 'deactivate', $actions ) && array_key_exists( $plugin_file, $this->plugins ) ) {
     53        public function filter_network_admin_plugin_action_links( $actions, $plugin ) {
     54            if ( plugin_basename( self::get( 'file' ) ) !== $plugin &&
     55                 array_key_exists( 'deactivate', $actions ) &&
     56                 array_key_exists( $plugin, $this->get_plugins() ) )
    11257                unset( $actions['deactivate'] );
    113             }
    11458
    11559            return $actions;
    11660        }
    11761
    118         public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) {
    119             if ( array_key_exists( 'deactivate', $actions ) && array_key_exists( $plugin_file, $this->plugins ) ) {
     62        public function filter_plugin_action_links( $actions, $plugin ) {
     63            if ( plugin_basename( self::get( 'file' ) ) !== $plugin &&
     64                 array_key_exists( 'deactivate', $actions ) &&
     65                 array_key_exists( $plugin, $this->get_plugins() ) )
    12066                unset( $actions['deactivate'] );
    121             }
    12267
    12368            return $actions;
  • cc-auto-activate-plugins/trunk/includes/class-plugin.php

    r1796330 r1797093  
    6666
    6767        public function __construct() {
    68             register_activation_hook( self::get( 'file' ), array( $this, 'activation' ) );
    69             register_deactivation_hook( self::get( 'file' ), array( $this, 'deactivation' ) );
     68            register_activation_hook( self::get( 'file' ), [ $this, 'activation' ] );
     69            register_deactivation_hook( self::get( 'file' ), [ $this, 'deactivation' ] );
    7070
    71             add_action( 'activated_plugin', array( $this, 'switch_plugin_hook' ), 10, 2 );
    72             add_action( 'deactivated_plugin', array( $this, 'switch_plugin_hook' ), 10, 2 );
     71            add_action( 'activated_plugin', [ $this, 'switch_plugin_hook' ], 10, 2 );
     72            add_action( 'deactivated_plugin', [ $this, 'switch_plugin_hook' ], 10, 2 );
    7373
    7474            $class = new ReflectionClass( $this );
     
    7979                    $args     = self::apply_filters( 'args', $method->getNumberOfParameters(), $class, $method );
    8080
    81                     add_filter( $hook, array( $this, $method->getName() ), $priority, $args );
     81                    add_filter( $hook, [ $this, $method->getName() ], $priority, $args );
    8282                }
    8383            }
     
    114114
    115115        protected function is_hook( $method ) {
    116             foreach ( array( 'filter_', 'action_' ) as $hook ) {
     116            foreach ( [ 'filter_', 'action_' ] as $hook ) {
    117117                if ( 0 === strpos( $method, $hook ) ) {
    118118                    return true;
     
    134134        }
    135135
    136         static public function get_template( $template, $vars = array() ) {
     136        static public function get_template( $template, $vars = [] ) {
    137137            $template = self::apply_filters( 'template', self::get( 'path' ) . '/templates/' . $template, $vars );
    138138            if ( ! is_file( $template ) ) {
     
    151151        }
    152152
    153         function switch_plugin_hook( $plugin, $network_wide = null ) {
     153        static public function get_sites( $args = [] ) {
     154            $args = wp_parse_args( $args, [ 'public' => 1 ] );
     155
     156            $sites = [];
     157            $sites = function_exists( 'get_sites' ) ? get_sites( $args ) : wp_get_sites( $args );
     158
     159            return $sites ? array_map( function( $site ) { return (array)$site; }, $sites ) : false;
     160        }
     161
     162        public function switch_plugin_hook( $plugin, $network_wide = null ) {
    154163            if ( ! $network_wide ) {
    155164                return;
     
    160169            $hook .= plugin_basename( self::get( 'file' ) );
    161170
    162             $this->call_user_func_array( 'do_action', array( $hook, false ) );
     171            $this->call_user_func_array( 'do_action', [ $hook, false ] );
    163172        }
    164173
    165         protected function call_user_func_array( $function, $args = array() ) {
     174        protected function call_user_func_array( $function, $args = [] ) {
    166175            if ( is_multisite() ) {
    167                 foreach ( wp_get_sites( array( 'public' => 1 ) ) as $blog ) {
    168                     switch_to_blog( $blog['blog_id'] );
     176                $sites = self::get_sites();
     177
     178                foreach ( $sites as $site ) {
     179                    switch_to_blog( $site['blog_id'] );
    169180                    call_user_func_array( $function, $args );
    170181                }
     182
    171183                restore_current_blog();
    172             } else {
    173                 $function( $args );
    174             }
     184            } else $function( $args );
    175185        }
    176186    }
  • cc-auto-activate-plugins/trunk/plugin.php

    r1796330 r1797093  
    55    Plugin URI: https://wordpress.org/plugins/cc-auto-activate-plugins
    66    Description: This plugin automatically activate all Plugins from WP_PLUGIN_DIR.
    7     Version: 1.0.0
     7    Version: 1.1.0
    88    Author: Clearcode
    99    Author URI: https://clearcode.cc
     
    4545}
    4646
    47 foreach ( array( 'singleton', 'plugin', 'auto-activate-plugins' ) as $class ) {
     47foreach ( [ 'singleton', 'plugin', 'auto-activate-plugins' ] as $class ) {
    4848    require_once( plugin_dir_path( __FILE__ ) . sprintf( 'includes/class-%s.php', $class ) );
    4949}
Note: See TracChangeset for help on using the changeset viewer.