| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Advanced WordPress Backgrounds |
| 4 |
* Description: Parallax, Video, Images Backgrounds |
| 5 |
* Version: 1.3.2 |
| 6 |
* Author: nK |
| 7 |
* Author URI: https://nkdev.info |
| 8 |
* License: GPLv2 or later |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 |
* Text Domain: advanced-backgrounds |
| 11 |
*/ |
| 12 |
|
| 13 |
// Make sure we don't expose any info if called directly |
| 14 |
if ( !function_exists( 'add_action' ) ) { |
| 15 |
echo 'Hi there! I\'m just a plugin, not much I can do when called directly.'; |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* nK Theme Helper Class |
| 22 |
*/ |
| 23 |
class nK_AWB { |
| 24 |
/** |
| 25 |
* The single class instance. |
| 26 |
*/ |
| 27 |
private static $_instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Main Instance |
| 31 |
* Ensures only one instance of this class exists in memory at any one time. |
| 32 |
*/ |
| 33 |
public static function instance () { |
| 34 |
if ( is_null( self::$_instance ) ) { |
| 35 |
self::$_instance = new self(); |
| 36 |
self::$_instance->init(); |
| 37 |
self::$_instance->init_hooks(); |
| 38 |
} |
| 39 |
return self::$_instance; |
| 40 |
} |
| 41 |
|
| 42 |
public $plugin_path; |
| 43 |
public $plugin_url; |
| 44 |
public $plugin_name; |
| 45 |
public $plugin_version; |
| 46 |
public $plugin_slug; |
| 47 |
public $plugin_name_sanitized; |
| 48 |
|
| 49 |
public function __construct() { |
| 50 |
/* We do nothing here! */ |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Init translation |
| 55 |
*/ |
| 56 |
public function init () { |
| 57 |
$this->plugin_path = plugin_dir_path(__FILE__); |
| 58 |
$this->plugin_url = plugin_dir_url(__FILE__); |
| 59 |
|
| 60 |
// get current plugin data |
| 61 |
$data = get_file_data(__FILE__, array( |
| 62 |
'Name' => 'Name', |
| 63 |
'Version' => 'Version', |
| 64 |
)); |
| 65 |
$this->plugin_name = $data['Name']; |
| 66 |
$this->plugin_version = $data['Version']; |
| 67 |
$this->plugin_slug = plugin_basename(__FILE__, '.php'); |
| 68 |
$this->plugin_name_sanitized = basename(__FILE__, '.php'); |
| 69 |
|
| 70 |
// load textdomain |
| 71 |
load_plugin_textdomain( 'advanced-backgrounds', false, dirname( plugin_basename(__FILE__) ) . '/languages/' ); |
| 72 |
|
| 73 |
// include helper files |
| 74 |
$this->include_dependencies(); |
| 75 |
|
| 76 |
// clear caches |
| 77 |
$this->clear_expired_caches(); |
| 78 |
|
| 79 |
// init classes |
| 80 |
new nK_AWB_Shortcode(); |
| 81 |
new nK_AWB_VC_Extend(); |
| 82 |
new nK_AWB_TinyMCE(); |
| 83 |
} |
| 84 |
|
| 85 |
public function init_hooks() { |
| 86 |
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register scripts that will be used in the future when portfolio will be printed. |
| 91 |
*/ |
| 92 |
public function register_scripts() { |
| 93 |
wp_register_script('jarallax', nk_awb()->plugin_url . 'assets/vendor/jarallax/jarallax.min.js', array('jquery'), '1.9.0', true); |
| 94 |
wp_register_script('jarallax-video', nk_awb()->plugin_url . 'assets/vendor/jarallax/jarallax-video.min.js', array('jarallax'), '1.9.0', true); |
| 95 |
wp_register_script('object-fit-images', nk_awb()->plugin_url . 'assets/vendor/object-fit-images/ofi.min.js', '', '', true); |
| 96 |
wp_register_script('nk-awb', nk_awb()->plugin_url . 'assets/awb/awb.js', array('jquery', 'jarallax', 'jarallax-video', 'object-fit-images'), nk_awb()->plugin_version, true); |
| 97 |
wp_register_style('nk-awb', nk_awb()->plugin_url . 'assets/awb/awb.css', '', nk_awb()->plugin_version); |
| 98 |
|
| 99 |
// add styles to header to fix image jumping |
| 100 |
wp_enqueue_style('nk-awb'); |
| 101 |
} |
| 102 |
|
| 103 |
// include |
| 104 |
private function include_dependencies () { |
| 105 |
require_once($this->plugin_path . 'classes/class-shortcode.php'); |
| 106 |
require_once($this->plugin_path . 'classes/class-vc-extend.php'); |
| 107 |
require_once($this->plugin_path . 'classes/class-tinymce.php'); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Options |
| 113 |
*/ |
| 114 |
private function get_options () { |
| 115 |
$options_slug = 'nk_awb_options'; |
| 116 |
return unserialize( get_option($options_slug, 'a:0:{}') ); |
| 117 |
} |
| 118 |
public function update_option ($name, $value) { |
| 119 |
$options_slug = 'nk_awb_options'; |
| 120 |
$options = self::get_options(); |
| 121 |
$options[self::sanitize_key($name)] = $value; |
| 122 |
update_option($options_slug, serialize($options)); |
| 123 |
} |
| 124 |
public function get_option ($name, $default = null) { |
| 125 |
$options = self::get_options(); |
| 126 |
$name = self::sanitize_key($name); |
| 127 |
return isset($options[$name]) ? $options[$name] : $default; |
| 128 |
} |
| 129 |
private function sanitize_key ($key) { |
| 130 |
return preg_replace( '/[^A-Za-z0-9\_]/i', '', str_replace( array( '-', ':' ), '_', $key ) ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Cache |
| 136 |
* $time in seconds |
| 137 |
*/ |
| 138 |
private function get_caches () { |
| 139 |
$caches_slug = 'cache'; |
| 140 |
return $this->get_option($caches_slug, array()); |
| 141 |
} |
| 142 |
public function set_cache ($name, $value, $time = 3600) { |
| 143 |
if(!$time || $time <= 0) { |
| 144 |
return; |
| 145 |
} |
| 146 |
$caches_slug = 'cache'; |
| 147 |
$caches = self::get_caches(); |
| 148 |
|
| 149 |
$caches[self::sanitize_key($name)] = array( |
| 150 |
'value' => $value, |
| 151 |
'expired' => time() + ((int) $time ? $time : 0) |
| 152 |
); |
| 153 |
$this->update_option($caches_slug, $caches); |
| 154 |
} |
| 155 |
public function get_cache ($name, $default = null) { |
| 156 |
$caches = self::get_caches(); |
| 157 |
$name = self::sanitize_key($name); |
| 158 |
return isset($caches[$name]['value']) ? $caches[$name]['value'] : $default; |
| 159 |
} |
| 160 |
public function clear_cache ($name) { |
| 161 |
$caches_slug = 'cache'; |
| 162 |
$caches = self::get_caches(); |
| 163 |
$name = self::sanitize_key($name); |
| 164 |
if(isset($caches[$name])) { |
| 165 |
$caches[$name] = null; |
| 166 |
$this->update_option($caches_slug, $caches); |
| 167 |
} |
| 168 |
} |
| 169 |
public function clear_expired_caches () { |
| 170 |
$caches_slug = 'cache'; |
| 171 |
$caches = self::get_caches(); |
| 172 |
foreach($caches as $k => $cache) { |
| 173 |
if(isset($cache) && isset($cache['expired']) && $cache['expired'] < time()) { |
| 174 |
$caches[$k] = null; |
| 175 |
} |
| 176 |
} |
| 177 |
$this->update_option($caches_slug, $caches); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
function nk_awb () { |
| 182 |
return nK_AWB::instance(); |
| 183 |
} |
| 184 |
add_action('plugins_loaded', 'nk_awb' ); |
| 185 |
|