| 1 |
<?php |
| 2 |
/** |
| 3 |
* The dependency model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 3.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
use ThemeisleSDK\Common\Abstract_Module; |
| 15 |
use ThemeisleSDK\Product; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Script loader module for ThemeIsle SDK. |
| 24 |
*/ |
| 25 |
class Script_Loader extends Abstract_Module { |
| 26 |
/** |
| 27 |
* Check if we should load the module for this product. |
| 28 |
* |
| 29 |
* @param Product $product Product to load the module for. |
| 30 |
* |
| 31 |
* @return bool Should we load ? |
| 32 |
*/ |
| 33 |
public function can_load( $product ) { |
| 34 |
if ( apply_filters( 'themeisle_sdk_ran_promos', false ) === true ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $this->is_from_partner( $product ) ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Load module logic. |
| 47 |
* |
| 48 |
* @param Product $product Product to load. |
| 49 |
* |
| 50 |
* @return Dependancy Module object. |
| 51 |
*/ |
| 52 |
public function load( $product ) { |
| 53 |
$this->product = $product; |
| 54 |
$this->setup_actions(); |
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Setup actions. |
| 60 |
*/ |
| 61 |
private function setup_actions() { |
| 62 |
add_filter( 'themeisle_sdk_dependency_script_handler', [ $this, 'get_script_handler' ], 10, 1 ); |
| 63 |
add_action( 'themeisle_sdk_dependency_enqueue_script', [ $this, 'enqueue_script' ], 10, 1 ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get the script handler. |
| 68 |
* |
| 69 |
* @param string $slug The slug of the script. |
| 70 |
* |
| 71 |
* @return string The script handler. Empty if slug is not a string or not implemented. |
| 72 |
*/ |
| 73 |
public function get_script_handler( $slug ) { |
| 74 |
if ( ! is_string( $slug ) ) { |
| 75 |
return ''; |
| 76 |
} |
| 77 |
|
| 78 |
if ( 'tracking' !== $slug && 'survey' !== $slug ) { |
| 79 |
return ''; |
| 80 |
} |
| 81 |
|
| 82 |
return apply_filters( 'themeisle_sdk_dependency_script_handler_name', 'themeisle_sdk_' . $slug . '_script', $slug ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Enqueue the script. |
| 87 |
* |
| 88 |
* @param string $slug The slug of the script. |
| 89 |
*/ |
| 90 |
public function enqueue_script( $slug ) { |
| 91 |
$handler = apply_filters( 'themeisle_sdk_dependency_script_handler', $slug ); |
| 92 |
if ( empty( $handler ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if ( 'tracking' === $slug ) { |
| 97 |
$this->load_tracking( $handler ); |
| 98 |
} elseif ( 'survey' === $slug ) { |
| 99 |
$this->load_survey( $handler ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Load the survey script. |
| 105 |
* |
| 106 |
* @param string $handler The script handler. |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
public function load_survey( $handler ) { |
| 111 |
global $themeisle_sdk_max_path; |
| 112 |
$asset_file = require $themeisle_sdk_max_path . '/assets/js/build/survey/survey_deps.asset.php'; |
| 113 |
|
| 114 |
wp_enqueue_script( |
| 115 |
$handler, |
| 116 |
$this->get_sdk_uri() . 'assets/js/build/survey/survey_deps.js', |
| 117 |
$asset_file['dependencies'], |
| 118 |
$asset_file['version'], |
| 119 |
true |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Load the tracking script. |
| 125 |
* |
| 126 |
* @param string $handler The script handler. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function load_tracking( $handler ) { |
| 131 |
global $themeisle_sdk_max_path; |
| 132 |
$asset_file = require $themeisle_sdk_max_path . '/assets/js/build/tracking/tracking.asset.php'; |
| 133 |
|
| 134 |
wp_enqueue_script( |
| 135 |
$handler, |
| 136 |
$this->get_sdk_uri() . 'assets/js/build/tracking/tracking.js', |
| 137 |
$asset_file['dependencies'], |
| 138 |
$asset_file['version'], |
| 139 |
true |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|