Last active
August 29, 2015 14:04
-
-
Save grappler/ec8c96db934bc3b14dcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name. | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://example.com | |
* @copyright 2014 Your Name or Company Name | |
*/ | |
/** | |
* Plugin class. This class should ideally be used to work with the | |
* public-facing side of the WordPress site. | |
* | |
* If you're interested in introducing administrative or dashboard | |
* functionality, then refer to `class-plugin-name-admin.php` | |
* | |
* @TODO: Rename this class to a proper name for your plugin. | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
*/ | |
class Plugin_Name { | |
/** | |
* Initialize the plugin by setting localization and loading public scripts | |
* and styles. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
// Activate plugin when new blog is added | |
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); | |
} | |
/** | |
* Fired when the plugin is activated. | |
* | |
* @since 1.0.0 | |
* | |
* @param boolean $network_wide True if WPMU superadmin uses | |
* "Network Activate" action, false if | |
* WPMU is disabled or plugin is | |
* activated on an individual blog. | |
*/ | |
public static function activate( $network_wide ) { | |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { | |
if ( $network_wide ) { | |
// Get all blog ids | |
$blog_ids = self::get_blog_ids(); | |
foreach ( $blog_ids as $blog_id ) { | |
switch_to_blog( $blog_id ); | |
self::single_activate(); | |
restore_current_blog(); | |
} | |
} else { | |
self::single_activate(); | |
} | |
} else { | |
self::single_activate(); | |
} | |
} | |
/** | |
* Fired when the plugin is deactivated. | |
* | |
* @since 1.0.0 | |
* | |
* @param boolean $network_wide True if WPMU superadmin uses | |
* "Network Deactivate" action, false if | |
* WPMU is disabled or plugin is | |
* deactivated on an individual blog. | |
*/ | |
public static function deactivate( $network_wide ) { | |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { | |
if ( $network_wide ) { | |
// Get all blog ids | |
$blog_ids = self::get_blog_ids(); | |
foreach ( $blog_ids as $blog_id ) { | |
switch_to_blog( $blog_id ); | |
self::single_deactivate(); | |
restore_current_blog(); | |
} | |
} else { | |
self::single_deactivate(); | |
} | |
} else { | |
self::single_deactivate(); | |
} | |
} | |
/** | |
* Fired when a new site is activated with a WPMU environment. | |
* | |
* @since 1.0.0 | |
* | |
* @param int $blog_id ID of the new blog. | |
*/ | |
public function activate_new_site( $blog_id ) { | |
if ( 1 !== did_action( 'wpmu_new_blog' ) ) { | |
return; | |
} | |
switch_to_blog( $blog_id ); | |
self::single_activate(); | |
restore_current_blog(); | |
} | |
/** | |
* Get all blog ids of blogs in the current network that are: | |
* - not archived | |
* - not spam | |
* - not deleted | |
* | |
* @since 1.0.0 | |
* | |
* @return array|false The blog ids, false if no matches. | |
*/ | |
private static function get_blog_ids() { | |
global $wpdb; | |
// get an array of blog ids | |
$sql = "SELECT blog_id | |
FROM $wpdb->blogs | |
WHERE archived = '0' | |
AND spam = '0' | |
AND deleted = '0'"; | |
$blogs = $wpdb->get_col( $wpdb->prepare( " | |
SELECT blog_id | |
FROM {$wpdb->blogs} | |
WHERE site_id = %d | |
AND blog_id <> %d | |
AND spam = '0' | |
AND deleted = '0' | |
AND archived = '0' | |
ORDER BY registered DESC | |
LIMIT %d, 5 | |
", $wpdb->siteid, $wpdb->blogid, $offset ) ); | |
return $wpdb->get_col( $sql ); | |
} | |
/** | |
* Fired for each blog when the plugin is activated. | |
* | |
* @since 1.0.0 | |
*/ | |
private static function single_activate() { | |
// @TODO: Define activation functionality here | |
} | |
/** | |
* Fired for each blog when the plugin is deactivated. | |
* | |
* @since 1.0.0 | |
*/ | |
private static function single_deactivate() { | |
// @TODO: Define deactivation functionality here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment