Changeset 1797093
- Timestamp:
- 01/04/2018 03:49:48 PM (8 years ago)
- Location:
- cc-auto-activate-plugins
- Files:
-
- 13 added
- 4 edited
-
tags/1.1.0 (added)
-
tags/1.1.0/.htaccess (added)
-
tags/1.1.0/AUTHORS.txt (added)
-
tags/1.1.0/LICENSE.txt (added)
-
tags/1.1.0/README.txt (added)
-
tags/1.1.0/includes (added)
-
tags/1.1.0/includes/class-auto-activate-plugins.php (added)
-
tags/1.1.0/includes/class-plugin.php (added)
-
tags/1.1.0/includes/class-singleton.php (added)
-
tags/1.1.0/includes/index.html (added)
-
tags/1.1.0/index.html (added)
-
tags/1.1.0/plugin.php (added)
-
tags/1.1.0/screenshot-1.png (added)
-
trunk/README.txt (modified) (2 diffs)
-
trunk/includes/class-auto-activate-plugins.php (modified) (1 diff)
-
trunk/includes/class-plugin.php (modified) (6 diffs)
-
trunk/plugin.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cc-auto-activate-plugins/trunk/README.txt
r1796330 r1797093 14 14 15 15 This plugin automatically activate all Plugins from WP_PLUGIN_DIR. 16 17 = Tips & Tricks = 18 19 1. 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). 20 2. 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 } );` 16 28 17 29 == Installation == … … 35 47 == Changelog == 36 48 49 = 1.1.0 = 50 *Release date: 04.01.2018* 51 52 * Added support for WordPress core 'activate_plugin' function. 53 37 54 = 1.0.0 = 38 55 *Release date: 02.01.2018* -
cc-auto-activate-plugins/trunk/includes/class-auto-activate-plugins.php
r1796330 r1797093 29 29 if ( ! class_exists( __NAMESPACE__ . '\Auto_Activate_Plugins' ) ) { 30 30 class Auto_Activate_Plugins extends Plugin { 31 protected $plugins = array();31 protected $plugins = []; 32 32 33 33 public function __construct() { 34 34 parent::__construct(); 35 35 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 ); 40 44 } 41 45 42 46 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() ); 65 51 } 66 52 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() ) ) 112 57 unset( $actions['deactivate'] ); 113 }114 58 115 59 return $actions; 116 60 } 117 61 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() ) ) 120 66 unset( $actions['deactivate'] ); 121 }122 67 123 68 return $actions; -
cc-auto-activate-plugins/trunk/includes/class-plugin.php
r1796330 r1797093 66 66 67 67 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' ] ); 70 70 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 ); 73 73 74 74 $class = new ReflectionClass( $this ); … … 79 79 $args = self::apply_filters( 'args', $method->getNumberOfParameters(), $class, $method ); 80 80 81 add_filter( $hook, array( $this, $method->getName() ), $priority, $args );81 add_filter( $hook, [ $this, $method->getName() ], $priority, $args ); 82 82 } 83 83 } … … 114 114 115 115 protected function is_hook( $method ) { 116 foreach ( array( 'filter_', 'action_' )as $hook ) {116 foreach ( [ 'filter_', 'action_' ] as $hook ) { 117 117 if ( 0 === strpos( $method, $hook ) ) { 118 118 return true; … … 134 134 } 135 135 136 static public function get_template( $template, $vars = array()) {136 static public function get_template( $template, $vars = [] ) { 137 137 $template = self::apply_filters( 'template', self::get( 'path' ) . '/templates/' . $template, $vars ); 138 138 if ( ! is_file( $template ) ) { … … 151 151 } 152 152 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 ) { 154 163 if ( ! $network_wide ) { 155 164 return; … … 160 169 $hook .= plugin_basename( self::get( 'file' ) ); 161 170 162 $this->call_user_func_array( 'do_action', array( $hook, false ));171 $this->call_user_func_array( 'do_action', [ $hook, false ] ); 163 172 } 164 173 165 protected function call_user_func_array( $function, $args = array()) {174 protected function call_user_func_array( $function, $args = [] ) { 166 175 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'] ); 169 180 call_user_func_array( $function, $args ); 170 181 } 182 171 183 restore_current_blog(); 172 } else { 173 $function( $args ); 174 } 184 } else $function( $args ); 175 185 } 176 186 } -
cc-auto-activate-plugins/trunk/plugin.php
r1796330 r1797093 5 5 Plugin URI: https://wordpress.org/plugins/cc-auto-activate-plugins 6 6 Description: This plugin automatically activate all Plugins from WP_PLUGIN_DIR. 7 Version: 1. 0.07 Version: 1.1.0 8 8 Author: Clearcode 9 9 Author URI: https://clearcode.cc … … 45 45 } 46 46 47 foreach ( array( 'singleton', 'plugin', 'auto-activate-plugins' )as $class ) {47 foreach ( [ 'singleton', 'plugin', 'auto-activate-plugins' ] as $class ) { 48 48 require_once( plugin_dir_path( __FILE__ ) . sprintf( 'includes/class-%s.php', $class ) ); 49 49 }
Note: See TracChangeset
for help on using the changeset viewer.