| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file that defines the core plugin class |
| 4 |
* |
| 5 |
* A class definition that includes attributes and functions used across both the |
| 6 |
* public-facing side of the site and the admin area. |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Core |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( 'Powerkit' ) ) { |
| 17 |
/** |
| 18 |
* Main Core Class |
| 19 |
*/ |
| 20 |
class Powerkit { |
| 21 |
/** |
| 22 |
* The plugin version number. |
| 23 |
* |
| 24 |
* @var string $data The plugin version number. |
| 25 |
*/ |
| 26 |
public $version; |
| 27 |
|
| 28 |
/** |
| 29 |
* The plugin data array. |
| 30 |
* |
| 31 |
* @var array $data The plugin data array. |
| 32 |
*/ |
| 33 |
public $data = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* The plugin settings array. |
| 37 |
* |
| 38 |
* @var array $settings The plugin data array. |
| 39 |
*/ |
| 40 |
public $settings = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* INIT (global theme queue) |
| 44 |
*/ |
| 45 |
public function init() { |
| 46 |
|
| 47 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 48 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 49 |
} |
| 50 |
|
| 51 |
// Get plugin data. |
| 52 |
$plugin_data = get_plugin_data( POWERKIT_PATH . '/powerkit.php' ); |
| 53 |
|
| 54 |
// Vars. |
| 55 |
$this->version = $plugin_data['Version']; |
| 56 |
|
| 57 |
// Settings. |
| 58 |
$this->settings = array( |
| 59 |
'name' => esc_html__( 'Powerkit', 'powerkit' ), |
| 60 |
'version' => $plugin_data['Version'], |
| 61 |
'documentation' => $plugin_data['AuthorURI'] . '/documentation/powerkit', |
| 62 |
); |
| 63 |
|
| 64 |
// Constants. |
| 65 |
$this->define( 'POWERKIT', true ); |
| 66 |
$this->define( 'POWERKIT_VERSION', $plugin_data['Version'] ); |
| 67 |
|
| 68 |
// Include core. |
| 69 |
require_once POWERKIT_PATH . 'core/core-powerkit-api.php'; |
| 70 |
require_once POWERKIT_PATH . 'core/core-powerkit-functions.php'; |
| 71 |
require_once POWERKIT_PATH . 'core/core-powerkit-helpers.php'; |
| 72 |
require_once POWERKIT_PATH . 'core/core-powerkit-post-meta.php'; |
| 73 |
|
| 74 |
// Include core classes. |
| 75 |
require_once POWERKIT_PATH . 'core/class-powerkit-module.php'; |
| 76 |
require_once POWERKIT_PATH . 'core/class-powerkit-module-admin.php'; |
| 77 |
require_once POWERKIT_PATH . 'core/class-powerkit-module-public.php'; |
| 78 |
require_once POWERKIT_PATH . 'core/class-powerkit-deprecated.php'; |
| 79 |
|
| 80 |
// Include admin classes. |
| 81 |
require_once POWERKIT_PATH . 'admin/class-admin-powerkit-manager.php'; |
| 82 |
|
| 83 |
// Include modules. |
| 84 |
powerkit_load_files( 'modules' ); |
| 85 |
|
| 86 |
// Include extensions. |
| 87 |
powerkit_load_files( 'extensions' ); |
| 88 |
|
| 89 |
// Actions. |
| 90 |
add_action( 'powerkit_plugin_activation', array( $this, 'activation' ) ); |
| 91 |
add_action( 'plugins_loaded', array( $this, 'check_version' ) ); |
| 92 |
add_action( 'amp_post_template_css', array( $this, 'amp_enqueue_styles' ) ); |
| 93 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 5 ); |
| 94 |
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ), 5 ); |
| 95 |
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) ); |
| 96 |
add_action( 'wp_head', array( $this, 'wp_head' ), 5 ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* This function will safely define a constant |
| 101 |
* |
| 102 |
* @param string $name The name. |
| 103 |
* @param mixed $value The value. |
| 104 |
*/ |
| 105 |
public function define( $name, $value = true ) { |
| 106 |
|
| 107 |
if ( ! defined( $name ) ) { |
| 108 |
define( $name, $value ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns true if has setting. |
| 114 |
* |
| 115 |
* @param string $name The name. |
| 116 |
*/ |
| 117 |
public function has_setting( $name ) { |
| 118 |
return isset( $this->settings[ $name ] ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns a setting. |
| 123 |
* |
| 124 |
* @param string $name The name. |
| 125 |
*/ |
| 126 |
public function get_setting( $name ) { |
| 127 |
return isset( $this->settings[ $name ] ) ? $this->settings[ $name ] : null; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Updates a setting. |
| 132 |
* |
| 133 |
* @param string $name The name. |
| 134 |
* @param mixed $value The value. |
| 135 |
*/ |
| 136 |
public function update_setting( $name, $value ) { |
| 137 |
$this->settings[ $name ] = $value; |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns data. |
| 143 |
* |
| 144 |
* @param string $name The name. |
| 145 |
*/ |
| 146 |
public function get_data( $name ) { |
| 147 |
return isset( $this->data[ $name ] ) ? $this->data[ $name ] : null; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Sets data. |
| 152 |
* |
| 153 |
* @param string $name The name. |
| 154 |
* @param mixed $value The value. |
| 155 |
*/ |
| 156 |
public function set_data( $name, $value ) { |
| 157 |
$this->data[ $name ] = $value; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Hook activation |
| 162 |
*/ |
| 163 |
public function activation() { |
| 164 |
if ( get_option( 'powerkit_db_version' ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
update_option( 'powerkit_db_version', powerkit_raw_setting( 'version' ), true ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Check current version |
| 173 |
*/ |
| 174 |
public function check_version() { |
| 175 |
|
| 176 |
// Version Data. |
| 177 |
$new = powerkit_raw_setting( 'version' ); |
| 178 |
|
| 179 |
// Get db version. |
| 180 |
$current = get_option( 'powerkit_db_version', $new ); |
| 181 |
|
| 182 |
// If versions don't match. |
| 183 |
if ( ! empty( $current ) && ! empty( $new ) && $current !== $new ) { |
| 184 |
/** |
| 185 |
* If different versions call a special hook. |
| 186 |
* |
| 187 |
* @param string $current Current version. |
| 188 |
* @param string $new New version. |
| 189 |
*/ |
| 190 |
do_action( 'powerkit_plugin_upgrade', $current, $new ); |
| 191 |
|
| 192 |
update_option( 'powerkit_db_version', $new ); |
| 193 |
|
| 194 |
} elseif ( ! empty( $new ) && $current !== $new ) { |
| 195 |
|
| 196 |
update_option( 'powerkit_db_version', $new ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* AMP stylesheets. |
| 202 |
* |
| 203 |
* @since 1.0.0 |
| 204 |
*/ |
| 205 |
public function amp_enqueue_styles() { |
| 206 |
echo file_get_contents( POWERKIT_PATH . 'assets/css/amp.css' ); // XSS. |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* This function will register scripts and styles for admin dashboard. |
| 211 |
* |
| 212 |
* @param string $page Current page. |
| 213 |
*/ |
| 214 |
public function admin_enqueue_scripts( $page ) { |
| 215 |
wp_enqueue_style( 'powerkit', POWERKIT_URL . 'assets/css/powerkit.css', array(), powerkit_get_setting( 'version' ) ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* This function will register scripts and styles |
| 220 |
*/ |
| 221 |
public function wp_enqueue_scripts() { |
| 222 |
// Scripts. |
| 223 |
if ( is_user_logged_in() ) { |
| 224 |
wp_enqueue_script( 'tippy', POWERKIT_URL . 'assets/js/tippy.all.min.js', array( 'jquery' ), powerkit_get_setting( 'version' ), true ); |
| 225 |
} |
| 226 |
|
| 227 |
wp_enqueue_script( 'powerkit', POWERKIT_URL . 'assets/js/_scripts.js', array( 'jquery', 'tippy' ), powerkit_get_setting( 'version' ), true ); |
| 228 |
|
| 229 |
// Styles. |
| 230 |
wp_enqueue_style( 'powerkit', powerkit_style( POWERKIT_URL . 'assets/css/powerkit.css' ), array(), powerkit_get_setting( 'version' ) ); |
| 231 |
|
| 232 |
// Add RTL support. |
| 233 |
wp_style_add_data( 'powerkit', 'rtl', 'replace' ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Sets up theme defaults and registers support for various WordPress features. |
| 238 |
*/ |
| 239 |
public function after_setup_theme() { |
| 240 |
// Register custom thumbnail sizes. |
| 241 |
add_image_size( 'pk-small', 80, 80, true ); |
| 242 |
add_image_size( 'pk-thumbnail', 300, 225, true ); |
| 243 |
|
| 244 |
// Add editor style. |
| 245 |
if ( is_rtl() ) { |
| 246 |
add_editor_style( powerkit_style( POWERKIT_URL . 'assets/css/powerkit-rtl.css' ) ); |
| 247 |
} else { |
| 248 |
add_editor_style( powerkit_style( POWERKIT_URL . 'assets/css/powerkit.css' ) ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Fire the wp_head action. |
| 254 |
*/ |
| 255 |
public function wp_head() { |
| 256 |
?> |
| 257 |
<link rel="preload" href="<?php echo esc_url( POWERKIT_URL . 'assets/fonts/powerkit-icons.woff' ); ?>" as="font" type="font/woff" crossorigin> |
| 258 |
<?php |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* The main function responsible for returning the one true powerkit Instance to functions everywhere. |
| 265 |
* Use this function like you would a global variable, except without needing to declare the global. |
| 266 |
* |
| 267 |
* Example: <?php $powerkit = powerkit(); ?> |
| 268 |
*/ |
| 269 |
function powerkit() { |
| 270 |
|
| 271 |
// Globals. |
| 272 |
global $powerkit_instance; |
| 273 |
|
| 274 |
// Init. |
| 275 |
if ( ! isset( $powerkit_instance ) ) { |
| 276 |
$powerkit_instance = new Powerkit(); |
| 277 |
$powerkit_instance->init(); |
| 278 |
} |
| 279 |
|
| 280 |
return $powerkit_instance; |
| 281 |
} |
| 282 |
|
| 283 |
// Initialize. |
| 284 |
powerkit(); |
| 285 |
} |
| 286 |
|