class-auxels.php
290 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Auxin Elements. |
| 4 | * |
| 5 | * @echo HEADER |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'AUXELS' ) ) : |
| 9 | |
| 10 | |
| 11 | |
| 12 | class AUXELS { |
| 13 | |
| 14 | /** |
| 15 | * Unique identifier for your plugin. |
| 16 | * |
| 17 | * The variable name is used as the text domain when internationalizing strings of text. |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $plugin_slug = 'auxin-elements'; |
| 24 | |
| 25 | /** |
| 26 | * Instance of this class. |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | * |
| 30 | * @var object |
| 31 | */ |
| 32 | protected static $instance = null; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Instance of Admin class. |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | * |
| 40 | * @var object |
| 41 | */ |
| 42 | public $admin = null; |
| 43 | |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Initialize the plugin |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | private function __construct() { |
| 52 | |
| 53 | $this->includes(); |
| 54 | |
| 55 | add_action( 'init', array( $this, 'init' ) ); |
| 56 | |
| 57 | // Activate plugin when new blog is added |
| 58 | add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); |
| 59 | |
| 60 | // Loaded action |
| 61 | do_action( 'auxels_loaded' ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @return [type] [description] |
| 68 | */ |
| 69 | private function includes() { |
| 70 | |
| 71 | // load common functionalities |
| 72 | include_once( AUXELS_INC_DIR . '/index.php' ); |
| 73 | |
| 74 | |
| 75 | // Dashboard and Administrative Functionality |
| 76 | if ( is_admin() ) { |
| 77 | |
| 78 | // Load AJAX spesific codes on demand |
| 79 | if ( defined('DOING_AJAX') && DOING_AJAX ){ |
| 80 | |
| 81 | } |
| 82 | |
| 83 | // Load admin spesific codes |
| 84 | else { |
| 85 | $this->admin = include( AUXELS_ADMIN_DIR . '/class-auxels-admin.php' ); |
| 86 | } |
| 87 | |
| 88 | // Load Frontend Functionality |
| 89 | } else { |
| 90 | include 'includes/class-auxels-frontend-assets.php'; |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * Init the plugin when WordPress Initialises. |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public function init(){ |
| 102 | |
| 103 | // @deprecate version 5.0 |
| 104 | global $wp_version; |
| 105 | if ( version_compare( $wp_version, '4.6', '<' ) ) { |
| 106 | // Load plugin text domain |
| 107 | $this->load_plugin_textdomain(); |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Return an instance of this class. |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | * |
| 118 | * @return object A single instance of this class. |
| 119 | */ |
| 120 | public static function get_instance() { |
| 121 | |
| 122 | // If the single instance hasn't been set, set it now. |
| 123 | if ( null == self::$instance ) { |
| 124 | self::$instance = new self; |
| 125 | } |
| 126 | |
| 127 | return self::$instance; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Fired when the plugin is activated. |
| 133 | * |
| 134 | * @since 1.0.0 |
| 135 | * |
| 136 | * @param boolean $network_wide True if WPMU superadmin uses |
| 137 | * "Network Activate" action, false if |
| 138 | * WPMU is disabled or plugin is |
| 139 | * activated on an individual blog. |
| 140 | */ |
| 141 | public static function activate( $network_wide ) { |
| 142 | |
| 143 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 144 | |
| 145 | if ( $network_wide ) { |
| 146 | |
| 147 | // Get all blog ids |
| 148 | $blog_ids = self::get_blog_ids(); |
| 149 | |
| 150 | foreach ( $blog_ids as $blog_id ) { |
| 151 | |
| 152 | switch_to_blog( $blog_id ); |
| 153 | self::single_activate(); |
| 154 | } |
| 155 | |
| 156 | restore_current_blog(); |
| 157 | |
| 158 | } else { |
| 159 | self::single_activate(); |
| 160 | } |
| 161 | |
| 162 | } else { |
| 163 | self::single_activate(); |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Fired when the plugin is deactivated. |
| 171 | * |
| 172 | * @since 1.0.0 |
| 173 | * |
| 174 | * @param boolean $network_wide True if WPMU superadmin uses |
| 175 | * "Network Deactivate" action, false if |
| 176 | * WPMU is disabled or plugin is |
| 177 | * deactivated on an individual blog. |
| 178 | */ |
| 179 | public static function deactivate( $network_wide ) { |
| 180 | |
| 181 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 182 | |
| 183 | if ( $network_wide ) { |
| 184 | |
| 185 | // Get all blog ids |
| 186 | $blog_ids = self::get_blog_ids(); |
| 187 | |
| 188 | foreach ( $blog_ids as $blog_id ) { |
| 189 | |
| 190 | switch_to_blog( $blog_id ); |
| 191 | self::single_deactivate(); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | restore_current_blog(); |
| 196 | |
| 197 | } else { |
| 198 | self::single_deactivate(); |
| 199 | } |
| 200 | |
| 201 | } else { |
| 202 | self::single_deactivate(); |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * Fired for each blog when the plugin is activated. |
| 210 | * |
| 211 | * @since 1.0.0 |
| 212 | */ |
| 213 | private static function single_activate() { |
| 214 | |
| 215 | do_action( 'auxels_activated', get_current_blog_id() ); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Fired for each blog when the plugin is deactivated. |
| 221 | * |
| 222 | * @since 1.0.0 |
| 223 | */ |
| 224 | private static function single_deactivate() { |
| 225 | do_action( 'auxels_deactivated' ); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Fired when a new site is activated with a WPMU environment. |
| 231 | * |
| 232 | * @since 1.0.0 |
| 233 | * |
| 234 | * @param int $blog_id ID of the new blog. |
| 235 | */ |
| 236 | public function activate_new_site( $blog_id ) { |
| 237 | |
| 238 | if ( 1 !== did_action( 'wpmu_new_blog' ) ) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | switch_to_blog( $blog_id ); |
| 243 | self::single_activate(); |
| 244 | restore_current_blog(); |
| 245 | |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Get all blog ids of blogs in the current network that are: |
| 250 | * - not archived |
| 251 | * - not spam |
| 252 | * - not deleted |
| 253 | * |
| 254 | * @since 1.0.0 |
| 255 | * |
| 256 | * @return array|false The blog ids, false if no matches. |
| 257 | */ |
| 258 | private static function get_blog_ids() { |
| 259 | |
| 260 | global $wpdb; |
| 261 | |
| 262 | // get an array of blog ids |
| 263 | $sql = "SELECT blog_id FROM $wpdb->blogs |
| 264 | WHERE archived = '0' AND spam = '0' |
| 265 | AND deleted = '0'"; |
| 266 | |
| 267 | return $wpdb->get_col( $sql ); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Load the plugin text domain for translation. |
| 274 | * |
| 275 | * @since 1.0.0 |
| 276 | */ |
| 277 | public function load_plugin_textdomain() { |
| 278 | |
| 279 | $locale = apply_filters( 'plugin_locale', get_locale(), AUXELS_TEXT_DOMAIN ); |
| 280 | load_textdomain( AUXELS_TEXT_DOMAIN, trailingslashit( WP_LANG_DIR ) . AUXELS_TEXT_DOMAIN . '/' . AUXELS_TEXT_DOMAIN . '-' . $locale . '.mo' ); |
| 281 | load_plugin_textdomain( AUXELS_TEXT_DOMAIN, FALSE, basename( AUXELS_DIR ) . '/languages/' ); |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | endif; |
| 287 | |
| 288 | function AUXELS(){ return AUXELS::get_instance(); } |
| 289 | AUXELS(); |
| 290 |