| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMPage |
| 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 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Sets our object instance and base class instance. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function set() |
| 23 |
{ |
| 24 |
$this->base = SGPMBase::getInstance(); |
| 25 |
} |
| 26 |
|
| 27 |
public function optionsSave() |
| 28 |
{ |
| 29 |
//CSRF check |
| 30 |
if (!check_admin_referer('sgpm_options_save', 'wp-nonce-token-options-save')) { |
| 31 |
wp_die('Security check fail'); |
| 32 |
} |
| 33 |
|
| 34 |
$diplayTarget = $this->getTargetData(); |
| 35 |
|
| 36 |
$popupId = $this->sanitize('sgpm-popup-id'); |
| 37 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 38 |
|
| 39 |
$options['popupsSettings'][$popupId]['displayTarget'] = $diplayTarget; |
| 40 |
update_option('sgpm_popup_maker_api_option', $options); |
| 41 |
|
| 42 |
wp_redirect(SGPM_ADMIN_URL."admin.php?page=popup-maker-api-settings&popupId=".$popupId); |
| 43 |
exit(); |
| 44 |
} |
| 45 |
|
| 46 |
public function generalSettingsSave() |
| 47 |
{ |
| 48 |
//CSRF check |
| 49 |
if (!check_admin_referer('sgpm_general_settings_save', 'wp-nonce-token-general_settings-save')) { |
| 50 |
wp_die('Security check fail'); |
| 51 |
} |
| 52 |
|
| 53 |
$selectedUserRoles = isset($_POST['sgpm-selected-user-roles']) ? array_map( 'sanitize_text_field', $_POST['sgpm-selected-user-roles'] ) : array(); |
| 54 |
update_option('sgpm_popup_maker_user_roles', $selectedUserRoles); |
| 55 |
|
| 56 |
// Save custom allowed tags |
| 57 |
$customAllowedTags = isset($_POST['sgpm-custom-allowed-tags']) ? sanitize_textarea_field($_POST['sgpm-custom-allowed-tags']) : ''; |
| 58 |
update_option('sgpm_popup_maker_custom_allowed_tags', $customAllowedTags); |
| 59 |
|
| 60 |
// Save custom allowed attributes |
| 61 |
$customAllowedAttrs = isset($_POST['sgpm-custom-allowed-attrs']) ? sanitize_textarea_field($_POST['sgpm-custom-allowed-attrs']) : ''; |
| 62 |
update_option('sgpm_popup_maker_custom_allowed_attrs', $customAllowedAttrs); |
| 63 |
|
| 64 |
wp_redirect(SGPM_ADMIN_URL."admin.php?page=popup-maker-api-settings"); |
| 65 |
} |
| 66 |
|
| 67 |
public function changePopupStatus() |
| 68 |
{ |
| 69 |
// AJAX check |
| 70 |
check_ajax_referer(SGPM_AJAX_NONCE, 'nonce_ajax'); |
| 71 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 72 |
$popupId = intval($this->sanitize('popupId')); |
| 73 |
$popupStatus = $this->sanitize('popupStatus'); |
| 74 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 75 |
$options['popupsSettings'][$popupId]['status'] = $popupStatus; |
| 76 |
if ($popupStatus == 'enabled' && !isset($options['popupsSettings'][$popupId]['displayTarget'])) { |
| 77 |
$options['popupsSettings'][$popupId]['displayTarget'] = $SGPM_DATA_CONFIG_ARRAY['displayTarget']['initialData']; |
| 78 |
} |
| 79 |
|
| 80 |
update_option('sgpm_popup_maker_api_option', $options); |
| 81 |
exit(); |
| 82 |
} |
| 83 |
|
| 84 |
public function init() |
| 85 |
{ |
| 86 |
$this->render(); |
| 87 |
} |
| 88 |
|
| 89 |
public function render() |
| 90 |
{ |
| 91 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 92 |
$ajax_nonce = wp_create_nonce(SGPM_AJAX_NONCE); |
| 93 |
if (isset($_GET['popupId']) && wp_verify_nonce($ajax_nonce, SGPM_AJAX_NONCE)) { |
| 94 |
$popupId = sanitize_text_field($_GET['popupId']); |
| 95 |
$popup = $options['popups'][$popupId]; |
| 96 |
if (isset($options['popupsSettings'][$popupId])) { |
| 97 |
$popupSettings = $options['popupsSettings'][$popupId]; |
| 98 |
} |
| 99 |
require_once(SGPM_VIEW.'sgpm_popup_edit_content.php'); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
require_once(SGPM_VIEW.'sgpm_page_content.php'); |
| 104 |
} |
| 105 |
|
| 106 |
public function sanitize($optionsKey) |
| 107 |
{ |
| 108 |
$ajax_nonce = wp_create_nonce(SGPM_AJAX_NONCE); |
| 109 |
if (isset($_POST[$optionsKey]) && wp_verify_nonce($ajax_nonce, SGPM_AJAX_NONCE)) { |
| 110 |
return sanitize_text_field($_POST[$optionsKey]); |
| 111 |
} |
| 112 |
else { |
| 113 |
return ""; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public function select2SearchData() |
| 118 |
{ |
| 119 |
// AJAX check |
| 120 |
check_ajax_referer(SGPM_AJAX_NONCE, 'nonce_ajax'); |
| 121 |
|
| 122 |
$objectKey = sanitize_text_field($_POST['objectKey']); |
| 123 |
$search = sanitize_text_field($_POST['searchTerm']); |
| 124 |
$objectKey = sanitize_text_field($_POST['objectKey']); |
| 125 |
$searchType = sanitize_text_field($_POST['searchType']); |
| 126 |
$search = sanitize_text_field($_POST['searchTerm']); |
| 127 |
|
| 128 |
$include = sanitize_text_field($_REQUEST['include']); |
| 129 |
$page = sanitize_text_field($_REQUEST['page']); |
| 130 |
|
| 131 |
if ($searchType == 'category') { |
| 132 |
|
| 133 |
$taxonomy = ! empty($objectKey) ? $objectKey : 'category'; |
| 134 |
|
| 135 |
$args = array( |
| 136 |
'search' => ! empty($search) ? $search : '', |
| 137 |
'include' => ! empty( $include ) ? $include : null, |
| 138 |
'page' => ! empty( $page ) ? absint( $page ) : null, |
| 139 |
'number' => 10, |
| 140 |
); |
| 141 |
|
| 142 |
$query = SGPMHelper::taxonomySelectlist($taxonomy, $args, true); |
| 143 |
|
| 144 |
foreach ( $query['items'] as $name => $id ) { |
| 145 |
$results['items'][] = array( |
| 146 |
'id' => $id, |
| 147 |
'text' => $name, |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
else { |
| 152 |
$args = array( |
| 153 |
's' => $search, |
| 154 |
'post__in' => ! empty( $include ) ? array_map( 'intval', $include ) : null, |
| 155 |
'page' => ! empty( $page ) ? absint( $page ) : null, |
| 156 |
'posts_per_page' => 10, |
| 157 |
'post_type' => $objectKey |
| 158 |
); |
| 159 |
|
| 160 |
$searchResults = SGPMHelper::getPostTypeData($args); |
| 161 |
|
| 162 |
if (empty($searchResults)) { |
| 163 |
$results['items'] = array(); |
| 164 |
} |
| 165 |
|
| 166 |
/*Selected custom post type convert for select2 format*/ |
| 167 |
foreach ($searchResults as $id => $name) { |
| 168 |
$results['items'][] = array( |
| 169 |
'id' => $id, |
| 170 |
'text' => $name |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
echo json_encode($results); |
| 176 |
wp_die(); |
| 177 |
} |
| 178 |
|
| 179 |
public function addConditionRuleRow() |
| 180 |
{ |
| 181 |
// AJAX check |
| 182 |
check_ajax_referer(SGPM_AJAX_NONCE, 'nonce_ajax'); |
| 183 |
|
| 184 |
$data = ''; |
| 185 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 186 |
$targetType = $this->sanitize('conditionName'); |
| 187 |
$ruleId = (int)sanitize_text_field($_POST['ruleId']); |
| 188 |
$conditionRule = $SGPM_DATA_CONFIG_ARRAY[$targetType]['initialData'][0]; |
| 189 |
$data .= SGPMCondition::createConditionRuleRow($conditionRule, $ruleId); |
| 190 |
|
| 191 |
$allowed_html = $this->allowed_html_tags(); |
| 192 |
echo wp_kses($data, $allowed_html); |
| 193 |
|
| 194 |
wp_die(); |
| 195 |
} |
| 196 |
|
| 197 |
public function allowed_html_tags() |
| 198 |
{ |
| 199 |
$allowedposttags = array(); |
| 200 |
$allowedposttags = wp_kses_allowed_html( 'post' ); |
| 201 |
$allowed_atts = array( |
| 202 |
'align' => array(), |
| 203 |
'class' => array(), |
| 204 |
'type' => array(), |
| 205 |
'id' => array(), |
| 206 |
'dir' => array(), |
| 207 |
'lang' => array(), |
| 208 |
'style' => array(), |
| 209 |
'xml:lang' => array(), |
| 210 |
'src' => array(), |
| 211 |
'alt' => array(), |
| 212 |
'href' => array(), |
| 213 |
'rel' => array(), |
| 214 |
'rev' => array(), |
| 215 |
'target' => array(), |
| 216 |
'novalidate' => array(), |
| 217 |
'type' => array(), |
| 218 |
'value' => array(), |
| 219 |
'name' => array(), |
| 220 |
'tabindex' => array(), |
| 221 |
'action' => array(), |
| 222 |
'method' => array(), |
| 223 |
'for' => array(), |
| 224 |
'width' => array(), |
| 225 |
'height' => array(), |
| 226 |
'data-*' => true, |
| 227 |
'title' => array(), |
| 228 |
'attr' => array(), |
| 229 |
'label' => array(), |
| 230 |
'selected' => array(), |
| 231 |
'multiple' => array() |
| 232 |
); |
| 233 |
|
| 234 |
// Tags de base |
| 235 |
$base_tags = array( |
| 236 |
'select', 'optgroup', 'option', 'form', 'label', 'input', 'textarea', |
| 237 |
'iframe', 'script', 'style', 'strong', 'small', 'table', 'span', |
| 238 |
'abbr', 'code', 'pre', 'div', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', |
| 239 |
'h6', 'ol', 'ul', 'li', 'em', 'hr', 'br', 'tr', 'td', 'p', 'a', 'b', 'i' |
| 240 |
); |
| 241 |
|
| 242 |
// Appliquer les tags de base |
| 243 |
foreach ($base_tags as $tag) { |
| 244 |
$allowedposttags[$tag] = $allowed_atts; |
| 245 |
} |
| 246 |
|
| 247 |
// Get and validate custom allowed tags |
| 248 |
$customAllowedTags = get_option('sgpm_popup_maker_custom_allowed_tags', ''); |
| 249 |
$customAllowedAttrs = get_option('sgpm_popup_maker_custom_allowed_attrs', ''); |
| 250 |
|
| 251 |
$validCustomTags = SGPMHelper::validateCustomTags($customAllowedTags); |
| 252 |
$validCustomAttrs = SGPMHelper::validateCustomAttrs($customAllowedAttrs); |
| 253 |
|
| 254 |
// Apply validated custom tags |
| 255 |
foreach ($validCustomTags as $tag) { |
| 256 |
$allowedposttags[$tag] = $allowed_atts; |
| 257 |
} |
| 258 |
|
| 259 |
// Apply validated custom attributes |
| 260 |
if (!empty($validCustomAttrs)) { |
| 261 |
$customAttrsArray = array(); |
| 262 |
foreach ($validCustomAttrs as $attr) { |
| 263 |
$customAttrsArray[$attr] = array(); |
| 264 |
} |
| 265 |
|
| 266 |
// Apply custom attributes to all tags |
| 267 |
foreach ($allowedposttags as $tag => $attrs) { |
| 268 |
$allowedposttags[$tag] = array_merge($attrs, $customAttrsArray); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return $allowedposttags; |
| 273 |
} |
| 274 |
|
| 275 |
public function changeConditionRuleRow() |
| 276 |
{ |
| 277 |
// AJAX check |
| 278 |
check_ajax_referer(SGPM_AJAX_NONCE, 'nonce_ajax'); |
| 279 |
|
| 280 |
$data = ''; |
| 281 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 282 |
|
| 283 |
$targetType = $this->sanitize('conditionName'); |
| 284 |
$conditionConfig = $SGPM_DATA_CONFIG_ARRAY[$targetType]; |
| 285 |
$ruleId = $this->sanitize('ruleId'); |
| 286 |
$paramName = sanitize_text_field($_POST['paramName']); |
| 287 |
$savedData = array( |
| 288 |
'param' => $paramName |
| 289 |
); |
| 290 |
|
| 291 |
if ($targetType == 'displayTarget' || $targetType == 'conditions') { |
| 292 |
$savedData['operator'] = '=='; |
| 293 |
} |
| 294 |
|
| 295 |
$savedData['value'] = $conditionConfig['paramsData'][$paramName]; |
| 296 |
$data .= SGPMCondition::createConditionRuleRow($savedData, $ruleId); |
| 297 |
|
| 298 |
$allowed_html = $this->allowed_html_tags(); |
| 299 |
echo wp_kses($data, $allowed_html); |
| 300 |
|
| 301 |
wp_die(); |
| 302 |
} |
| 303 |
|
| 304 |
private function getTargetData() |
| 305 |
{ |
| 306 |
$ajax_nonce = wp_create_nonce(SGPM_AJAX_NONCE); |
| 307 |
if (!wp_verify_nonce($ajax_nonce, SGPM_AJAX_NONCE)) { |
| 308 |
return array(); |
| 309 |
} |
| 310 |
|
| 311 |
$conditionType = sanitize_text_field($_POST['sgpm-condition-type']); |
| 312 |
$targetData = $_POST['sgpm-display-target']; |
| 313 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 314 |
$targetConfig = $SGPM_DATA_CONFIG_ARRAY['displayTarget']; |
| 315 |
$paramsData = $targetConfig['paramsData']; |
| 316 |
$attrs = $targetConfig['attrs']; |
| 317 |
$popupTarget = array(); |
| 318 |
|
| 319 |
foreach ($targetData as $ruleId => $ruleData) { |
| 320 |
|
| 321 |
if (empty($ruleData['value']) && !is_null($paramsData[$ruleData['param']])) { |
| 322 |
$targetData[$ruleId]['value'] = ''; |
| 323 |
} |
| 324 |
|
| 325 |
if (isset($ruleData['value']) && is_array($ruleData['value'])) { |
| 326 |
|
| 327 |
$valueAttrs = @$attrs[$ruleData['param']]; |
| 328 |
$postType = sanitize_text_field(@$valueAttrs['data-value-param']); |
| 329 |
$isNotPostType = sanitize_text_field(@$valueAttrs['isNotPostType']); |
| 330 |
$objectType = sanitize_text_field(@$valueAttrs['data-value-type']); |
| 331 |
|
| 332 |
if (empty($valueAttrs['isNotPostType'])) { |
| 333 |
$isNotPostType = false; |
| 334 |
} |
| 335 |
|
| 336 |
if ($objectType == 'category') { |
| 337 |
$searchResults = array(); |
| 338 |
foreach ($ruleData['value'] as $key => $catId) { |
| 339 |
|
| 340 |
$catId = sanitize_text_field($catId); |
| 341 |
$cat = get_term_by('id', $catId, $postType); |
| 342 |
$searchResults[$catId] = $cat->name; |
| 343 |
|
| 344 |
} |
| 345 |
$targetData[$ruleId]['value'] = $searchResults; |
| 346 |
} |
| 347 |
else if (!$isNotPostType) { |
| 348 |
$args = array( |
| 349 |
'post__in' => array_values(array_map( 'sanitize_text_field', $ruleData['value'])), |
| 350 |
'posts_per_page' => 10, |
| 351 |
'post_type' => $postType |
| 352 |
); |
| 353 |
$searchResults = SGPMHelper::getPostTypeData($args); |
| 354 |
$targetData[$ruleId]['value'] = $searchResults; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if ($conditionType == 'everywhere') { |
| 360 |
$targetData[] = array( |
| 361 |
'condition_type' => $conditionType |
| 362 |
); |
| 363 |
} |
| 364 |
|
| 365 |
return $targetData; |
| 366 |
} |
| 367 |
} |
| 368 |
|