| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMOutput |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Reference to base plugin class. |
| 7 |
* |
| 8 |
* @var SGPMBase |
| 9 |
*/ |
| 10 |
protected $base; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
$this->set(); |
| 15 |
add_action('wp_head', array($this, 'output')); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Sets our object instance and base class instance. |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
public function set() |
| 24 |
{ |
| 25 |
$this->base = SGPMBase::getInstance(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* attach popup on website. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function output() |
| 34 |
{ |
| 35 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 36 |
$postId = $pageId = get_the_ID(); |
| 37 |
$popupHashIds = array(); |
| 38 |
|
| 39 |
foreach ($options['popups'] as $popupId => $popup) { |
| 40 |
if (!isset($options['popupsSettings'][$popupId])) continue; |
| 41 |
$popupSettings = $options['popupsSettings'][$popupId]; |
| 42 |
if (!isset($popupSettings['status']) || $popupSettings['status'] == 'disabled') continue; |
| 43 |
$displayTargets = $popupSettings['displayTarget']; |
| 44 |
|
| 45 |
foreach ($popupSettings['displayTarget'] as $displayTarget) { |
| 46 |
if (isset($displayTarget['condition_type']) && $displayTarget['condition_type'] == 'everywhere') { |
| 47 |
$displayTargets = $displayTarget['condition_type']; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ($this->allowToSetPopup($displayTargets)) { |
| 52 |
$popupHashIds[] = $popup['hashId']; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if (!empty($popupHashIds)) { |
| 57 |
|
| 58 |
wp_enqueue_script( 'sgpm-default-embed-code', SGPM_ASSETS_URL.'js/defaultEmbedCode.js', array(), '1.0.0', false ); |
| 59 |
wp_localize_script( 'sgpm-default-embed-code', 'sgpmPopupHashIds', |
| 60 |
$popupHashIds |
| 61 |
); |
| 62 |
// Use wp_add_inline_script for simple string values (WordPress 5.7+ requirement) |
| 63 |
wp_add_inline_script( 'sgpm-default-embed-code', 'var SGPM_SERVICE_URL = "' . esc_js( SGPM_SERVICE_URL ) . '";', 'before' ); |
| 64 |
wp_add_inline_script( 'sgpm-default-embed-code', 'var SGPM_ASSETS_URL = "' . esc_js( SGPM_ASSETS_URL ) . '";', 'before' ); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
private function allowToSetPopup($displayTargets) |
| 71 |
{ |
| 72 |
if ($displayTargets == 'everywhere') { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
$targetData = $this->divideIntoPermissiveAndForbidden($displayTargets); |
| 77 |
$isPostInForbidden = $this->isPostInForbidden($targetData); |
| 78 |
if ($isPostInForbidden) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
$isPermissive = $this->isPermissive($targetData); |
| 82 |
return $isPermissive; |
| 83 |
} |
| 84 |
|
| 85 |
public function divideIntoPermissiveAndForbidden($postMetaData) |
| 86 |
{ |
| 87 |
$permissive = array(); |
| 88 |
$forbidden = array(); |
| 89 |
|
| 90 |
foreach ($postMetaData as $data) { |
| 91 |
|
| 92 |
if (empty($data['operator'])) { |
| 93 |
break; |
| 94 |
} |
| 95 |
|
| 96 |
if ($data['operator'] == '==') { |
| 97 |
$permissive[] = $data; |
| 98 |
} |
| 99 |
else { |
| 100 |
$forbidden[] = $data; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$postMetaDivideData = array( |
| 105 |
'permissive' => $permissive, |
| 106 |
'forbidden' => $forbidden |
| 107 |
); |
| 108 |
|
| 109 |
return $postMetaDivideData; |
| 110 |
} |
| 111 |
|
| 112 |
private function isPostInForbidden($target) |
| 113 |
{ |
| 114 |
$isForbidden = false; |
| 115 |
|
| 116 |
if (empty($target['forbidden'])) { |
| 117 |
return $isForbidden; |
| 118 |
} |
| 119 |
|
| 120 |
foreach ($target['forbidden'] as $targetData) { |
| 121 |
if ($this->isInitPopup($targetData)) { |
| 122 |
$isForbidden = true; |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $isForbidden; |
| 128 |
} |
| 129 |
|
| 130 |
private function isPermissive($target) |
| 131 |
{ |
| 132 |
$isPermissive = false; |
| 133 |
|
| 134 |
if (empty($target['permissive'])) { |
| 135 |
$isPermissive = true; |
| 136 |
return $isPermissive; |
| 137 |
} |
| 138 |
|
| 139 |
foreach ($target['permissive'] as $targetData) { |
| 140 |
if ($this->isInitPopup($targetData)) { |
| 141 |
$isPermissive = true; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return $isPermissive; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
private function isInitPopup($targetData) |
| 151 |
{ |
| 152 |
$isInit = false; |
| 153 |
$postId = $pageId = get_the_ID(); |
| 154 |
$pageForPosts = get_option('page_for_posts'); |
| 155 |
|
| 156 |
if (strpos($targetData['param'], '_all')) { |
| 157 |
$endIndex = strpos($targetData['param'], '_all'); |
| 158 |
$postType = substr($targetData['param'], 0, $endIndex); |
| 159 |
$currentPostType = get_post_type($postId); |
| 160 |
|
| 161 |
if ($postType == $currentPostType) { |
| 162 |
$isInit = true; |
| 163 |
} |
| 164 |
} |
| 165 |
// for woocomerce |
| 166 |
else if (strpos($targetData['param'], '_selected_category')) { |
| 167 |
$terms = get_the_terms($postId, 'product_cat'); |
| 168 |
if ($terms) { |
| 169 |
foreach ($terms as $key => $term) { |
| 170 |
if (in_array($term->name, $targetData['value'])) { |
| 171 |
$isInit = true; |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
else if (strpos($targetData['param'], '_selected')) { |
| 178 |
$values = array(); |
| 179 |
|
| 180 |
if (!empty($targetData['value'])) { |
| 181 |
$values = array_keys($targetData['value']); |
| 182 |
} |
| 183 |
|
| 184 |
if (in_array($postId, $values) || (is_Home() && in_array($pageForPosts, $values))) { |
| 185 |
$isInit = true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
else if ($targetData['param'] == 'post_type' && !empty($targetData['value'])) { |
| 190 |
$selectedCustomPostTypes = array_values($targetData['value']); |
| 191 |
$currentPostType = get_post_type($postId); |
| 192 |
|
| 193 |
if (in_array($currentPostType, $selectedCustomPostTypes)) { |
| 194 |
$isInit = true; |
| 195 |
} |
| 196 |
} |
| 197 |
else if ($targetData['param'] == 'post_category' && !empty($targetData['value'])) { |
| 198 |
$values = $targetData['value']; |
| 199 |
$currentPostCategories = get_the_category($postId); |
| 200 |
|
| 201 |
foreach ($currentPostCategories as $categoryName) { |
| 202 |
if (in_array($categoryName->term_id, $values)) { |
| 203 |
$isInit = true; |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
else if ($targetData['param'] == 'page_type' && !empty($targetData['value'])) { |
| 210 |
$postTypes = $targetData['value']; |
| 211 |
foreach ($postTypes as $postType) { |
| 212 |
|
| 213 |
if ($postType == 'is_home_page') { |
| 214 |
if (is_front_page() && is_home()) { |
| 215 |
// Default homepage |
| 216 |
$isInit = true; |
| 217 |
break; |
| 218 |
} elseif (is_front_page()) { |
| 219 |
// static homepage |
| 220 |
$isInit = true; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
else if ($postType()) { |
| 225 |
$isInit = true; |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
else if ($targetData['param'] == 'page_template' && !empty($targetData['value'])) { |
| 231 |
|
| 232 |
$currentPageTemplate = basename(get_page_template()); |
| 233 |
if (in_array($currentPageTemplate, $targetData['value'])) { |
| 234 |
$isInit = true; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $isInit; |
| 239 |
} |
| 240 |
} |
| 241 |
|