class-auxels.php
345 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 | |
| 58 | // Activate plugin when new blog is added |
| 59 | add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); |
| 60 | |
| 61 | // Loaded action |
| 62 | do_action( 'auxels_loaded' ); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * |
| 68 | * @return [type] [description] |
| 69 | */ |
| 70 | private function includes() { |
| 71 | |
| 72 | // Auto-load classes on demand |
| 73 | if ( function_exists( "__autoload" ) ) { |
| 74 | spl_autoload_register( "__autoload" ); |
| 75 | } |
| 76 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 77 | |
| 78 | // Load components |
| 79 | require( AUXELS_DIR . '/vendor/autoload.php' ); |
| 80 | |
| 81 | // load common functionalities |
| 82 | include_once( AUXELS_INC_DIR . '/index.php' ); |
| 83 | |
| 84 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 85 | include( AUXELS_ADMIN_DIR . '/includes/classes/class-auxin-cli-commands.php' ); |
| 86 | } |
| 87 | |
| 88 | // Dashboard and Administrative Functionality |
| 89 | if ( is_admin() ) { |
| 90 | |
| 91 | // Load AJAX spesific codes on demand |
| 92 | if ( defined('DOING_AJAX') && DOING_AJAX ){ |
| 93 | include( AUXELS_ADMIN_DIR . '/includes/admin-ajax.php' ); |
| 94 | include( AUXELS_ADMIN_DIR . '/includes/admin-the-functions.php' ); |
| 95 | include( 'includes/frontend-ajax.php' ); |
| 96 | } |
| 97 | |
| 98 | // Load admin spesific codes |
| 99 | $this->admin = include( AUXELS_ADMIN_DIR . '/class-auxels-admin.php' ); |
| 100 | |
| 101 | // Load Frontend Functionality |
| 102 | } else { |
| 103 | include 'includes/index.php'; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Auto-load classes on demand to reduce memory consumption |
| 112 | * |
| 113 | * @param mixed $class |
| 114 | * @return void |
| 115 | */ |
| 116 | public function autoload( $class ) { |
| 117 | $path = null; |
| 118 | $class = strtolower( $class ); |
| 119 | $file = 'class-' . str_replace( '_', '-', $class ) . '.php'; |
| 120 | |
| 121 | // the possible pathes containing classes |
| 122 | $possible_pathes = array( |
| 123 | AUXELS_INC_DIR . '/classes/', |
| 124 | AUXELS_ADMIN_DIR . '/includes/classes/' |
| 125 | ); |
| 126 | |
| 127 | foreach ( $possible_pathes as $path ) { |
| 128 | if( is_readable( $path . $file ) ){ |
| 129 | include_once( $path . $file ); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Init the plugin when WordPress Initialises. |
| 141 | * |
| 142 | * @return void |
| 143 | */ |
| 144 | public function init(){ |
| 145 | // Load plugin text domain |
| 146 | $this->load_plugin_textdomain(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Return an instance of this class. |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | * |
| 155 | * @return object A single instance of this class. |
| 156 | */ |
| 157 | public static function get_instance() { |
| 158 | |
| 159 | // If the single instance hasn't been set, set it now. |
| 160 | if ( null == self::$instance ) { |
| 161 | self::$instance = new self; |
| 162 | } |
| 163 | |
| 164 | return self::$instance; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Fired when the plugin is activated. |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | * |
| 173 | * @param boolean $network_wide True if WPMU superadmin uses |
| 174 | * "Network Activate" action, false if |
| 175 | * WPMU is disabled or plugin is |
| 176 | * activated on an individual blog. |
| 177 | */ |
| 178 | public static function activate( $network_wide ) { |
| 179 | |
| 180 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 181 | |
| 182 | if ( $network_wide ) { |
| 183 | |
| 184 | // Get all blog ids |
| 185 | $blog_ids = self::get_blog_ids(); |
| 186 | |
| 187 | foreach ( $blog_ids as $blog_id ) { |
| 188 | |
| 189 | switch_to_blog( $blog_id ); |
| 190 | self::single_activate(); |
| 191 | } |
| 192 | |
| 193 | restore_current_blog(); |
| 194 | |
| 195 | } else { |
| 196 | self::single_activate(); |
| 197 | } |
| 198 | |
| 199 | } else { |
| 200 | self::single_activate(); |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Fired when the plugin is deactivated. |
| 208 | * |
| 209 | * @since 1.0.0 |
| 210 | * |
| 211 | * @param boolean $network_wide True if WPMU superadmin uses |
| 212 | * "Network Deactivate" action, false if |
| 213 | * WPMU is disabled or plugin is |
| 214 | * deactivated on an individual blog. |
| 215 | */ |
| 216 | public static function deactivate( $network_wide ) { |
| 217 | |
| 218 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 219 | |
| 220 | if ( $network_wide ) { |
| 221 | |
| 222 | // Get all blog ids |
| 223 | $blog_ids = self::get_blog_ids(); |
| 224 | |
| 225 | foreach ( $blog_ids as $blog_id ) { |
| 226 | |
| 227 | switch_to_blog( $blog_id ); |
| 228 | self::single_deactivate(); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | restore_current_blog(); |
| 233 | |
| 234 | } else { |
| 235 | self::single_deactivate(); |
| 236 | } |
| 237 | |
| 238 | } else { |
| 239 | self::single_deactivate(); |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * Fired for each blog when the plugin is activated. |
| 247 | * |
| 248 | * @since 1.0.0 |
| 249 | */ |
| 250 | private static function single_activate() { |
| 251 | add_action( 'after_setup_theme', array( 'AUXELS', 'flush' ) ); |
| 252 | |
| 253 | do_action( 'auxels_activated', get_current_blog_id() ); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Fired for each blog when the plugin is deactivated. |
| 259 | * |
| 260 | * @since 1.0.0 |
| 261 | */ |
| 262 | private static function single_deactivate() { |
| 263 | do_action( 'auxels_deactivated' ); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Fired when a new site is activated with a WPMU environment. |
| 269 | * |
| 270 | * @since 1.0.0 |
| 271 | * |
| 272 | * @param int $blog_id ID of the new blog. |
| 273 | */ |
| 274 | public function activate_new_site( $blog_id ) { |
| 275 | |
| 276 | if ( 1 !== did_action( 'wpmu_new_blog' ) ) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | switch_to_blog( $blog_id ); |
| 281 | self::single_activate(); |
| 282 | restore_current_blog(); |
| 283 | |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Get all blog ids of blogs in the current network that are: |
| 288 | * - not archived |
| 289 | * - not spam |
| 290 | * - not deleted |
| 291 | * |
| 292 | * @since 1.0.0 |
| 293 | * |
| 294 | * @return array|false The blog ids, false if no matches. |
| 295 | */ |
| 296 | private static function get_blog_ids() { |
| 297 | |
| 298 | global $wpdb; |
| 299 | |
| 300 | // get an array of blog ids |
| 301 | $sql = "SELECT blog_id FROM $wpdb->blogs |
| 302 | WHERE archived = '0' AND spam = '0' |
| 303 | AND deleted = '0'"; |
| 304 | |
| 305 | return $wpdb->get_col( $sql ); |
| 306 | |
| 307 | } |
| 308 | /** |
| 309 | * Get the template path. |
| 310 | * @return string |
| 311 | */ |
| 312 | public function template_path() { |
| 313 | return apply_filters( 'auxin_elements_template_path', AUXELS_PUB_DIR . '/templates/' ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Flush and perform some tasks on theme setup |
| 318 | * |
| 319 | * @since 1.0.0 |
| 320 | */ |
| 321 | public static function flush() { |
| 322 | // try to regenerate the asset files on plugin activation |
| 323 | auxin_add_custom_js(); |
| 324 | auxin_add_custom_css(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Load the plugin text domain for translation. |
| 329 | * |
| 330 | * @since 1.0.0 |
| 331 | */ |
| 332 | public function load_plugin_textdomain() { |
| 333 | |
| 334 | $locale = apply_filters( 'plugin_locale', get_locale(), 'auxin-elements' ); |
| 335 | load_textdomain( 'auxin-elements', trailingslashit( WP_LANG_DIR ) . 'auxin-elements' . '/' . 'auxin-elements' . '-' . $locale . '.mo' ); |
| 336 | load_plugin_textdomain( 'auxin-elements', false, basename( AUXELS_DIR ) . '/languages/' ); |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | |
| 341 | endif; |
| 342 | |
| 343 | function AUXELS(){ return AUXELS::get_instance(); } |
| 344 | AUXELS(); |
| 345 |