| 1 |
<?php |
| 2 |
/** |
| 3 |
* The promotions model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* Here's how to hook it in your plugin: add_filter( 'menu_icons_load_promotions', function() { return array( 'otter' ); } ); |
| 6 |
* |
| 7 |
* @package ThemeIsleSDK |
| 8 |
* @subpackage Modules |
| 9 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 10 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace ThemeisleSDK\Modules; |
| 15 |
|
| 16 |
use ThemeisleSDK\Common\Abstract_Module; |
| 17 |
use ThemeisleSDK\Product; |
| 18 |
|
| 19 |
// Exit if accessed directly. |
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Promotions module for ThemeIsle SDK. |
| 26 |
*/ |
| 27 |
class Promotions extends Abstract_Module { |
| 28 |
/** |
| 29 |
* Fetched feeds items. |
| 30 |
* |
| 31 |
* @var array Feed items. |
| 32 |
*/ |
| 33 |
private $promotions_to_load = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Should we load this module. |
| 37 |
* |
| 38 |
* @param Product $product Product object. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public function can_load( $product ) { |
| 43 |
if ( $this->is_from_partner( $product ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$this->promotions_to_load = apply_filters( $product->get_key() . '_load_promotions', array() ); |
| 48 |
|
| 49 |
if ( 0 === count( $this->promotions_to_load ) ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Registers the hooks. |
| 58 |
* |
| 59 |
* @param Product $product Product to load. |
| 60 |
* |
| 61 |
* @return Promotions Module instance. |
| 62 |
*/ |
| 63 |
public function load( $product ) { |
| 64 |
if ( 0 === count( $this->promotions_to_load ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! $this->is_writeable() || ! current_user_can( 'install_plugins' ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$this->product = $product; |
| 73 |
|
| 74 |
add_action( 'init', array( $this, 'register_settings' ), 99 ); |
| 75 |
add_action( 'admin_init', array( $this, 'register_reference' ), 99 ); |
| 76 |
|
| 77 |
if ( in_array( 'otter', $this->promotions_to_load ) |
| 78 |
&& false === apply_filters( 'themeisle_sdk_load_promotions_otter', false ) |
| 79 |
&& ! ( defined( 'OTTER_BLOCKS_VERSION' ) |
| 80 |
|| $this->is_plugin_installed( 'otter-blocks' ) ) |
| 81 |
&& version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) { |
| 82 |
add_filter( 'themeisle_sdk_load_promotions_otter', '__return_true' ); |
| 83 |
|
| 84 |
if ( false !== $this->show_otter_promotion() ) { |
| 85 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Register plugin reference. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function register_reference() { |
| 98 |
|
| 99 |
$reference_key = ! isset( $_GET['reference_key'] ) ? '' : sanitize_key( $_GET['reference_key'] ); |
| 100 |
if ( empty( $reference_key ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
if ( get_option( 'otter_reference_key', false ) !== false ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
update_option( 'otter_reference_key', $reference_key ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Register Settings |
| 111 |
* |
| 112 |
* @since 1.2.0 |
| 113 |
* @access public |
| 114 |
*/ |
| 115 |
public function register_settings() { |
| 116 |
register_setting( |
| 117 |
'themeisle_sdk_settings', |
| 118 |
'themeisle_sdk_promotions_otter', |
| 119 |
array( |
| 120 |
'type' => 'string', |
| 121 |
'sanitize_callback' => 'sanitize_text_field', |
| 122 |
'show_in_rest' => true, |
| 123 |
'default' => '{}', |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
register_setting( |
| 128 |
'themeisle_sdk_settings', |
| 129 |
'themeisle_sdk_promotions_otter_installed', |
| 130 |
array( |
| 131 |
'type' => 'boolean', |
| 132 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 133 |
'show_in_rest' => true, |
| 134 |
'default' => false, |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get the Otter Blocks plugin status. |
| 141 |
* |
| 142 |
* @param string $plugin Plugin slug. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
private function is_plugin_installed( $plugin ) { |
| 147 |
static $allowed_keys = [ 'otter-blocks' => 'otter-blocks/otter-blocks.php' ]; |
| 148 |
if ( ! isset( $allowed_keys[ $plugin ] ) ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
if ( file_exists( WP_CONTENT_DIR . '/plugins/' . $allowed_keys[ $plugin ] ) ) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get status of Otter promotion message. |
| 160 |
* |
| 161 |
* @return mixed |
| 162 |
*/ |
| 163 |
public function show_otter_promotion() { |
| 164 |
$promotions = array( |
| 165 |
'blocks_css', |
| 166 |
'blocks_animation', |
| 167 |
'blocks_conditions', |
| 168 |
); |
| 169 |
|
| 170 |
$option = json_decode( get_option( 'themeisle_sdk_promotions_otter', '{}' ), true ); |
| 171 |
|
| 172 |
if ( 0 === count( $option ) ) { |
| 173 |
return 'blocks-css'; |
| 174 |
} |
| 175 |
|
| 176 |
if ( isset( $option['blocks-css'] ) && ! isset( $option['blocks-animation'] ) && $option['blocks-css'] < strtotime( '-7 days' ) ) { |
| 177 |
return 'blocks-animation'; |
| 178 |
} |
| 179 |
|
| 180 |
if ( isset( $option['blocks-animation'] ) && ! isset( $option['blocks-conditions'] ) && $option['blocks-animation'] < strtotime( '-7 days' ) ) { |
| 181 |
return 'blocks-conditions'; |
| 182 |
} |
| 183 |
|
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check if the path is writable. |
| 189 |
* |
| 190 |
* @return boolean |
| 191 |
* @access public |
| 192 |
*/ |
| 193 |
public function is_writeable() { |
| 194 |
|
| 195 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 196 |
$filesystem_method = get_filesystem_method(); |
| 197 |
|
| 198 |
if ( 'direct' === $filesystem_method ) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Load Gutenberg editor assets. |
| 206 |
* |
| 207 |
* @access public |
| 208 |
*/ |
| 209 |
public function enqueue_editor_assets() { |
| 210 |
global $themeisle_sdk_max_path; |
| 211 |
|
| 212 |
$themeisle_sdk_path = dirname( $themeisle_sdk_max_path ); |
| 213 |
$themeisle_sdk_src = plugins_url( '/', $themeisle_sdk_max_path ); |
| 214 |
|
| 215 |
$asset_file = include $themeisle_sdk_path . '/themeisle-sdk/assets/js/build/index.asset.php'; |
| 216 |
|
| 217 |
wp_enqueue_script( |
| 218 |
'themeisle-sdk-otter-promotions', |
| 219 |
$themeisle_sdk_src . 'themeisle-sdk/assets/js/build/index.js', |
| 220 |
array_merge( $asset_file['dependencies'], [ 'updates' ] ), |
| 221 |
$asset_file['version'], |
| 222 |
true |
| 223 |
); |
| 224 |
|
| 225 |
$option = get_option( 'themeisle_sdk_promotions_otter', '{}' ); |
| 226 |
|
| 227 |
wp_localize_script( |
| 228 |
'themeisle-sdk-otter-promotions', |
| 229 |
'themeisleSDKPromotions', |
| 230 |
array( |
| 231 |
'product' => $this->product->get_name(), |
| 232 |
'assets' => $themeisle_sdk_src . 'themeisle-sdk/assets/images/', |
| 233 |
'showPromotion' => $this->show_otter_promotion(), |
| 234 |
'promotions_otter' => $option, |
| 235 |
'activationUrl' => esc_url( |
| 236 |
add_query_arg( |
| 237 |
array( |
| 238 |
'plugin_status' => 'all', |
| 239 |
'paged' => '1', |
| 240 |
'action' => 'activate', |
| 241 |
'reference_key' => $this->product->get_key(), |
| 242 |
'plugin' => rawurlencode( 'otter-blocks/otter-blocks.php' ), |
| 243 |
'_wpnonce' => wp_create_nonce( 'activate-plugin_otter-blocks/otter-blocks.php' ), |
| 244 |
), |
| 245 |
admin_url( 'plugins.php' ) |
| 246 |
) |
| 247 |
), |
| 248 |
) |
| 249 |
); |
| 250 |
} |
| 251 |
} |
| 252 |
|