| 1 |
<?php |
| 2 |
|
| 3 |
////////////////////////////////////////////////////////////// |
| 4 |
//=========================================================== |
| 5 |
// class.php |
| 6 |
//=========================================================== |
| 7 |
// PAGELAYER |
| 8 |
// Inspired by the DESIRE to be the BEST OF ALL |
| 9 |
// ---------------------------------------------------------- |
| 10 |
// Started by: Pulkit Gupta |
| 11 |
// Date: 23rd Jan 2017 |
| 12 |
// Time: 23:00 hrs |
| 13 |
// Site: http://pagelayer.com/wordpress (PAGELAYER) |
| 14 |
// ---------------------------------------------------------- |
| 15 |
// Please Read the Terms of use at http://pagelayer.com/tos |
| 16 |
// ---------------------------------------------------------- |
| 17 |
//=========================================================== |
| 18 |
// (c)Pagelayer Team |
| 19 |
//=========================================================== |
| 20 |
////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
// Are we being accessed directly ? |
| 23 |
if(!defined('PAGELAYER_VERSION')) { |
| 24 |
exit('Hacking Attempt !'); |
| 25 |
} |
| 26 |
|
| 27 |
// Get the client IP |
| 28 |
function _pagelayer_getip(){ |
| 29 |
if(isset($_SERVER["REMOTE_ADDR"])){ |
| 30 |
return $_SERVER["REMOTE_ADDR"]; |
| 31 |
}elseif(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
| 32 |
return $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 33 |
}elseif(isset($_SERVER["HTTP_CLIENT_IP"])){ |
| 34 |
return $_SERVER["HTTP_CLIENT_IP"]; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
// Get the client IP |
| 39 |
function pagelayer_getip(){ |
| 40 |
|
| 41 |
global $pagelayer; |
| 42 |
|
| 43 |
// Just so that we have something |
| 44 |
$ip = _pagelayer_getip(); |
| 45 |
|
| 46 |
$pagelayer['ip_method'] = (int) @$pagelayer['ip_method']; |
| 47 |
|
| 48 |
if(isset($_SERVER["REMOTE_ADDR"])){ |
| 49 |
$ip = $_SERVER["REMOTE_ADDR"]; |
| 50 |
} |
| 51 |
|
| 52 |
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && @$pagelayer['ip_method'] == 1){ |
| 53 |
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
| 54 |
} |
| 55 |
|
| 56 |
if(isset($_SERVER["HTTP_CLIENT_IP"]) && @$pagelayer['ip_method'] == 2){ |
| 57 |
$ip = $_SERVER["HTTP_CLIENT_IP"]; |
| 58 |
} |
| 59 |
|
| 60 |
// Hacking fix for X-Forwarded-For |
| 61 |
if(!pagelayer_valid_ip($ip)){ |
| 62 |
return ''; |
| 63 |
} |
| 64 |
|
| 65 |
return $ip; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
// Execute a select query and return an array |
| 70 |
function pagelayer_selectquery($query, $array = 0){ |
| 71 |
global $wpdb; |
| 72 |
|
| 73 |
$result = $wpdb->get_results($query, 'ARRAY_A'); |
| 74 |
|
| 75 |
if(empty($array)){ |
| 76 |
return current($result); |
| 77 |
}else{ |
| 78 |
return $result; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Check if an IP is valid |
| 83 |
function pagelayer_valid_ip($ip){ |
| 84 |
|
| 85 |
// IPv6 |
| 86 |
if(pagelayer_valid_ipv6($ip)){ |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
// IPv4 |
| 91 |
if(!ip2long($ip)){ |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
function pagelayer_valid_ipv6($ip){ |
| 99 |
|
| 100 |
$pattern = '/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/'; |
| 101 |
|
| 102 |
if(!preg_match($pattern, $ip)){ |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
return true; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
// Check if a field is posted via POST else return default value |
| 111 |
function pagelayer_optpost($name, $default = ''){ |
| 112 |
|
| 113 |
if(!empty($_POST[$name])){ |
| 114 |
return pagelayer_inputsec(pagelayer_htmlizer(trim($_POST[$name]))); |
| 115 |
} |
| 116 |
|
| 117 |
return $default; |
| 118 |
} |
| 119 |
|
| 120 |
// Check if a field is posted via GET else return default value |
| 121 |
function pagelayer_optget($name, $default = ''){ |
| 122 |
|
| 123 |
if(!empty($_GET[$name])){ |
| 124 |
return pagelayer_inputsec(pagelayer_htmlizer(trim($_GET[$name]))); |
| 125 |
} |
| 126 |
|
| 127 |
return $default; |
| 128 |
} |
| 129 |
|
| 130 |
// Check if a field is posted via GET or POST else return default value |
| 131 |
function pagelayer_optreq($name, $default = ''){ |
| 132 |
|
| 133 |
if(!empty($_REQUEST[$name])){ |
| 134 |
return pagelayer_inputsec(pagelayer_htmlizer(trim($_REQUEST[$name]))); |
| 135 |
} |
| 136 |
|
| 137 |
return $default; |
| 138 |
} |
| 139 |
|
| 140 |
// For filling in posted values |
| 141 |
function pagelayer_POSTval($name, $default = ''){ |
| 142 |
|
| 143 |
return (!empty($_POST) ? (!isset($_POST[$name]) ? '' : $_POST[$name]) : $default); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
function pagelayer_POSTchecked($name, $default = false){ |
| 148 |
|
| 149 |
return (!empty($_POST) ? (isset($_POST[$name]) ? 'checked="checked"' : '') : (!empty($default) ? 'checked="checked"' : '')); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
function pagelayer_POSTselect($name, $value, $default = false){ |
| 154 |
|
| 155 |
if(empty($_POST)){ |
| 156 |
if(!empty($default)){ |
| 157 |
return 'selected="selected"'; |
| 158 |
} |
| 159 |
}else{ |
| 160 |
if(isset($_POST[$name])){ |
| 161 |
if(trim($_POST[$name]) == $value){ |
| 162 |
return 'selected="selected"'; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
function pagelayer_inputsec($string){ |
| 170 |
|
| 171 |
if(!get_magic_quotes_gpc()){ |
| 172 |
|
| 173 |
$string = addslashes($string); |
| 174 |
|
| 175 |
}else{ |
| 176 |
|
| 177 |
$string = stripslashes($string); |
| 178 |
$string = addslashes($string); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
// This is to replace ` which can cause the command to be executed in exec() |
| 183 |
$string = str_replace('`', '\`', $string); |
| 184 |
|
| 185 |
return $string; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
function pagelayer_htmlizer($string){ |
| 190 |
|
| 191 |
$string = htmlentities($string, ENT_QUOTES, 'UTF-8'); |
| 192 |
|
| 193 |
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches); |
| 194 |
|
| 195 |
foreach($matches[1] as $mk => $mv){ |
| 196 |
$tmp_m = pagelayer_entity_check($matches[2][$mk]); |
| 197 |
$string = str_replace($matches[1][$mk], $tmp_m, $string); |
| 198 |
} |
| 199 |
|
| 200 |
return $string; |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
function pagelayer_entity_check($string){ |
| 205 |
|
| 206 |
//Convert Hexadecimal to Decimal |
| 207 |
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string); |
| 208 |
|
| 209 |
//Squares and Spaces - return nothing |
| 210 |
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : '&#'.$num.';'); |
| 211 |
|
| 212 |
return $string; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
// Check if a checkbox is selected |
| 217 |
function pagelayer_is_checked($post){ |
| 218 |
|
| 219 |
if(!empty($_POST[$post])){ |
| 220 |
return true; |
| 221 |
} |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
// Reoort an error |
| 226 |
function pagelayer_report_error($error = array()){ |
| 227 |
|
| 228 |
if(empty($error)){ |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
$error_string = '<b>Please fix the below error(s) :</b> <br />'; |
| 233 |
|
| 234 |
foreach($error as $ek => $ev){ |
| 235 |
$error_string .= '* '.$ev.'<br />'; |
| 236 |
} |
| 237 |
|
| 238 |
echo '<div id="message" class="error"><p>' |
| 239 |
. __pl($error_string) |
| 240 |
. '</p></div>'; |
| 241 |
} |
| 242 |
|
| 243 |
// Report a notice |
| 244 |
function pagelayer_report_notice($notice = array()){ |
| 245 |
|
| 246 |
global $wp_version; |
| 247 |
|
| 248 |
if(empty($notice)){ |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
// Which class do we have to use ? |
| 253 |
if(version_compare($wp_version, '3.8', '<')){ |
| 254 |
$notice_class = 'updated'; |
| 255 |
}else{ |
| 256 |
$notice_class = 'updated'; |
| 257 |
} |
| 258 |
|
| 259 |
$notice_string = '<b>Please check the below notice(s) :</b> <br />'; |
| 260 |
|
| 261 |
foreach($notice as $ek => $ev){ |
| 262 |
$notice_string .= '* '.$ev.'<br />'; |
| 263 |
} |
| 264 |
|
| 265 |
echo '<div id="message" class="'.$notice_class.'"><p>' |
| 266 |
. __pl($notice_string) |
| 267 |
. '</p></div>'; |
| 268 |
} |
| 269 |
|
| 270 |
// Convert an objext to array |
| 271 |
function pagelayer_objectToArray($d){ |
| 272 |
|
| 273 |
if(is_object($d)){ |
| 274 |
$d = get_object_vars($d); |
| 275 |
} |
| 276 |
|
| 277 |
if(is_array($d)){ |
| 278 |
return array_map(__FUNCTION__, $d); // recursive |
| 279 |
}elseif(is_object($d)){ |
| 280 |
return pagelayer_objectToArray($d); |
| 281 |
}else{ |
| 282 |
return $d; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
// Sanitize variables |
| 287 |
function pagelayer_sanitize_variables($variables = array()){ |
| 288 |
|
| 289 |
if(is_array($variables)){ |
| 290 |
foreach($variables as $k => $v){ |
| 291 |
$variables[$k] = trim($v); |
| 292 |
$variables[$k] = escapeshellcmd($v); |
| 293 |
} |
| 294 |
}else{ |
| 295 |
$variables = escapeshellcmd(trim($variables)); |
| 296 |
} |
| 297 |
|
| 298 |
return $variables; |
| 299 |
} |
| 300 |
|
| 301 |
// Is multisite ? |
| 302 |
function pagelayer_is_multisite() { |
| 303 |
|
| 304 |
if(function_exists('get_site_option') && function_exists('is_multisite') && is_multisite()){ |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
// Generate a random string |
| 312 |
function pagelayer_RandomString($length = 10){ |
| 313 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 314 |
$charactersLength = strlen($characters); |
| 315 |
$randomString = ''; |
| 316 |
for($i = 0; $i < $length; $i++){ |
| 317 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 318 |
} |
| 319 |
return $randomString; |
| 320 |
} |
| 321 |
|
| 322 |
function pagelayer_print($array){ |
| 323 |
|
| 324 |
echo '<pre>'; |
| 325 |
print_r($array); |
| 326 |
echo '</pre>'; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
function pagelayer_cleanpath($path){ |
| 331 |
$path = str_replace('\\\\', '/', $path); |
| 332 |
$path = str_replace('\\', '/', $path); |
| 333 |
$path = str_replace('//', '/', $path); |
| 334 |
return rtrim($path, '/'); |
| 335 |
} |
| 336 |
|
| 337 |
// Returns the Numeric Value of results Per Page |
| 338 |
function pagelayer_get_page($get = 'page', $resperpage = 50){ |
| 339 |
|
| 340 |
$resperpage = (!empty($_REQUEST['reslen']) && is_numeric($_REQUEST['reslen']) ? (int) pagelayer_optreq('reslen') : $resperpage); |
| 341 |
|
| 342 |
if(pagelayer_optget($get)){ |
| 343 |
$pg = (int) pagelayer_optget($get); |
| 344 |
$pg = $pg - 1; |
| 345 |
$page = ($pg * $resperpage); |
| 346 |
$page = ($page <= 0 ? 0 : $page); |
| 347 |
}else{ |
| 348 |
$page = 0; |
| 349 |
} |
| 350 |
return $page; |
| 351 |
} |
| 352 |
|
| 353 |
// Are we editing from the Admin panel ? |
| 354 |
function pagelayer_is_editing($force = false){ |
| 355 |
|
| 356 |
global $post, $pagelayer; |
| 357 |
|
| 358 |
if(!empty($force)){ |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
if(!is_admin()){ |
| 363 |
return false; |
| 364 |
} |
| 365 |
|
| 366 |
$current_file = basename($_SERVER['PHP_SELF']); |
| 367 |
|
| 368 |
$type = get_post_type(); |
| 369 |
//echo $type;return false; |
| 370 |
//$page = pagelayer_optreq('page'); |
| 371 |
|
| 372 |
// Are we in the live editor mode OR is this a post which is supported |
| 373 |
if((pagelayer_supported_type($type) && in_array($current_file, array('post.php', 'post-new.php'))) || pagelayer_is_live()){ |
| 374 |
return true; |
| 375 |
}else{ |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
// Is the given post type editable by us ? |
| 382 |
function pagelayer_supported_type($type){ |
| 383 |
|
| 384 |
global $pagelayer; |
| 385 |
|
| 386 |
$type = trim($type); |
| 387 |
|
| 388 |
if(in_array($type, $pagelayer->settings['post_types'])){ |
| 389 |
return true; |
| 390 |
} |
| 391 |
|
| 392 |
return false; |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
function pagelayer_shortlink($id){ |
| 397 |
$link = wp_get_shortlink($id); |
| 398 |
$link .= substr_count($link, '?') > 0 ? '' : '?'; |
| 399 |
return $link; |
| 400 |
} |
| 401 |
|
| 402 |
// Are we in live mode ? |
| 403 |
function pagelayer_is_live(){ |
| 404 |
|
| 405 |
global $post; |
| 406 |
|
| 407 |
// Are we seeing the post ? |
| 408 |
if(!isset($post) || !isset($post->ID) || empty($post->ID)){ |
| 409 |
return false; |
| 410 |
} |
| 411 |
|
| 412 |
// Is it the live mode ? |
| 413 |
if(pagelayer_optreq('pagelayer-live')){ |
| 414 |
return true; |
| 415 |
} |
| 416 |
|
| 417 |
return false; |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
// Are we in live IFRAME mode ? |
| 422 |
function pagelayer_is_live_iframe(){ |
| 423 |
|
| 424 |
// Are we seeing the post ? |
| 425 |
if(!pagelayer_is_live()){ |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
// Is it the live mode ? |
| 430 |
if(pagelayer_optreq('pagelayer-iframe')){ |
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
return false; |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
// Can the current user edit the post ? |
| 439 |
function pagelayer_user_can_edit($post = NULL){ |
| 440 |
|
| 441 |
global $wp_the_query, $current_user; |
| 442 |
|
| 443 |
if(!isset($post) || empty($post) || $post === NULL){ |
| 444 |
global $post; |
| 445 |
} |
| 446 |
|
| 447 |
wp_get_current_user(); |
| 448 |
|
| 449 |
if(isset($post) && is_object($post) && isset($post->ID) && isset($post->post_author) && isset($current_user) && is_object($current_user) && isset($current_user->ID) && (current_user_can('edit_others_posts', $post->ID) || ($post->post_author == $current_user->ID))){ |
| 450 |
return true; |
| 451 |
} |
| 452 |
|
| 453 |
return false; |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
// Language sting function |
| 458 |
function __pl($key){ |
| 459 |
|
| 460 |
global $pagelayer; |
| 461 |
|
| 462 |
if(!empty($pagelayer->l[$key])){ |
| 463 |
return $pagelayer->l[$key]; |
| 464 |
} |
| 465 |
|
| 466 |
return $key; |
| 467 |
|
| 468 |
} |
| 469 |
|
| 470 |
// Give the list of icon sources |
| 471 |
function pagelayer_icon_sources(){ |
| 472 |
return array(); |
| 473 |
} |
| 474 |
|
| 475 |
// Loads the shortcodes |
| 476 |
function pagelayer_load_shortcodes(){ |
| 477 |
|
| 478 |
include_once(PAGELAYER_DIR.'/main/shortcode_functions.php'); |
| 479 |
include_once(PAGELAYER_DIR.'/main/shortcodes.php'); |
| 480 |
|
| 481 |
// pQuery |
| 482 |
include_once(PAGELAYER_DIR.'/lib/pquery/IQuery.php'); |
| 483 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_formatter.php'); |
| 484 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_node_html.php'); |
| 485 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_tokenizer.php'); |
| 486 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_parser_html.php'); |
| 487 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_selector_html.php'); |
| 488 |
include_once(PAGELAYER_DIR.'/lib/pquery/gan_xml2array.php'); |
| 489 |
include_once(PAGELAYER_DIR.'/lib/pquery/pQuery.php'); |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
// Add the shortcodes to the pagelayer list |
| 494 |
function pagelayer_add_shortcode($tag, $params = array()){ |
| 495 |
|
| 496 |
global $pagelayer; |
| 497 |
|
| 498 |
// Is there a handler function ? |
| 499 |
if(!empty($params['func'])){ |
| 500 |
add_shortcode($tag, 'pagelayer_render_shortcode');//$params['func']); |
| 501 |
//unset($params['func']); |
| 502 |
} |
| 503 |
|
| 504 |
// Is there a group ? |
| 505 |
if(empty($params['group'])){ |
| 506 |
$params['group'] = 'misc'; |
| 507 |
} |
| 508 |
|
| 509 |
// Add the advanced styling group |
| 510 |
$params['options'] = [ |
| 511 |
'ele_bg_styles' => __pl('ele_bg_styles'), |
| 512 |
'ele_styles' => __pl('ele_styles'), |
| 513 |
'border_styles' => __pl('border_styles'), |
| 514 |
'animation_styles' => __pl('animation_styles'), |
| 515 |
'responsive_styles' => __pl('responsive_styles'), |
| 516 |
'custom_styles' => __pl('custom_styles'), |
| 517 |
]; |
| 518 |
|
| 519 |
// Are the settings there which hold the params ? |
| 520 |
if(empty($params['settings'])){ |
| 521 |
$params['settings'] = [ |
| 522 |
'params' => $params['name'], |
| 523 |
]; |
| 524 |
} |
| 525 |
|
| 526 |
// Disable the style options |
| 527 |
if(!empty($params['styles'])){ |
| 528 |
$params['settings'] = array_merge($params['settings'], $params['styles']); |
| 529 |
unset($params['styles']); |
| 530 |
} |
| 531 |
|
| 532 |
// Insert the shortcode |
| 533 |
$pagelayer->shortcodes[$tag] = $params; |
| 534 |
$pagelayer->groups[$params['group']][] = $tag; |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
// Returns the Image values |
| 539 |
function pagelayer_image($id){ |
| 540 |
|
| 541 |
$ret = []; |
| 542 |
|
| 543 |
// External image ? |
| 544 |
if(pagelayer_is_external_img($id)){ |
| 545 |
|
| 546 |
$ret['full-url'] = $id; |
| 547 |
|
| 548 |
// Attachment |
| 549 |
}elseif(!empty($id)){ |
| 550 |
|
| 551 |
$id = (int) @$id; |
| 552 |
|
| 553 |
$image = get_post($id); |
| 554 |
|
| 555 |
// Is there an attachment which is an image ? |
| 556 |
if(!empty($image) && $image->post_type == 'attachment' && wp_attachment_is_image($id)){ |
| 557 |
|
| 558 |
$sizes = get_intermediate_image_sizes(); |
| 559 |
array_unshift($sizes, 'full'); |
| 560 |
|
| 561 |
foreach($sizes as $size){ |
| 562 |
$src = wp_get_attachment_image_src($id, $size); |
| 563 |
$ret[$size.'-url'] = $src[0]; |
| 564 |
} |
| 565 |
|
| 566 |
// Title and Alt |
| 567 |
$title = esc_attr($image->post_title); |
| 568 |
$alt = get_post_meta($id, '_wp_attachment_image_alt', true); |
| 569 |
$alt = empty($alt) ? $image->post_excerpt : $alt; |
| 570 |
$alt = empty($alt) ? $image->post_title : $alt; |
| 571 |
$alt = empty($alt) ? '' : trim(strip_tags($alt)); |
| 572 |
$link = get_attachment_link($id); |
| 573 |
$caption = wp_get_attachment_caption($id); |
| 574 |
$caption = !empty($caption) ? $caption : ''; |
| 575 |
|
| 576 |
$ret['alt'] = $alt; |
| 577 |
$ret['title'] = $title; |
| 578 |
$ret['link'] = $link; |
| 579 |
$ret['caption'] = $caption; |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
// No image |
| 586 |
if(empty($ret['full-url'])){ |
| 587 |
$ret['full-url'] = PAGELAYER_URL.'/images/default-image.png'; |
| 588 |
} |
| 589 |
|
| 590 |
$ret['url'] = $ret['full-url']; |
| 591 |
|
| 592 |
return $ret; |
| 593 |
|
| 594 |
} |
| 595 |
|
| 596 |
// Checks if the given parameter is an external link or a wp attachment id |
| 597 |
function pagelayer_is_external_img($img = ''){ |
| 598 |
|
| 599 |
if(empty($img)){ |
| 600 |
return false; |
| 601 |
} |
| 602 |
|
| 603 |
if(preg_match('#http://#is', $img) || preg_match('#https://#is', $img) || preg_match('#^{{#is', $img)){ |
| 604 |
return true; |
| 605 |
} |
| 606 |
|
| 607 |
return false; |
| 608 |
|
| 609 |
} |
| 610 |
|
| 611 |
// Returns the attachment url |
| 612 |
function pagelayer_attachment($id){ |
| 613 |
|
| 614 |
$ret = []; |
| 615 |
|
| 616 |
// External url ? |
| 617 |
if(pagelayer_is_external_img($id)){ |
| 618 |
|
| 619 |
$ret['url'] = $id; |
| 620 |
|
| 621 |
// Attachment |
| 622 |
}elseif(!empty($id)){ |
| 623 |
|
| 624 |
$ret['url'] = wp_get_attachment_url($id); |
| 625 |
|
| 626 |
} |
| 627 |
|
| 628 |
return $ret; |
| 629 |
|
| 630 |
} |
| 631 |
|
| 632 |
// Convert the regular URL of a Video to a Embed URL |
| 633 |
// Todo : Check |
| 634 |
function pagelayer_video_url($source){ |
| 635 |
|
| 636 |
if (!empty($source)) { |
| 637 |
$source = filter_var($source, FILTER_SANITIZE_URL); |
| 638 |
$source = str_replace('&', '&', $source); |
| 639 |
$url = parse_url($source); |
| 640 |
|
| 641 |
$youtubeRegExp = '/youtube\.com|youtu\.be/is'; |
| 642 |
$vimeoRegExp = '/vimeo\.com/is'; |
| 643 |
|
| 644 |
if (preg_match($youtubeRegExp, $source)) { |
| 645 |
$videoSite = 'youtube'; |
| 646 |
} else if (preg_match($vimeoRegExp, $source)) { |
| 647 |
$videoSite = 'vimeo'; |
| 648 |
} |
| 649 |
|
| 650 |
switch ($videoSite) { |
| 651 |
case 'youtube': |
| 652 |
|
| 653 |
if (preg_match('/youtube\.com/is', $source)) { |
| 654 |
|
| 655 |
if (preg_match('/watch/is', $source)) { |
| 656 |
parse_str($url['query'], $parameters); |
| 657 |
|
| 658 |
if (isset($parameters['v']) && !empty($parameters['v'])) { |
| 659 |
$videoId = $parameters['v']; |
| 660 |
} |
| 661 |
|
| 662 |
} else if (preg_match('/embed/is', $url['path'])) { |
| 663 |
$path = explode('/', $url['path']); |
| 664 |
if (isset($path[2]) && !empty($path[2])) { |
| 665 |
$videoId = $path[2]; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
} else if (preg_match('/youtu\.be/is', $url['host'])) { |
| 670 |
$path = explode('/', $url['path']); |
| 671 |
|
| 672 |
if (isset($path[1]) && !empty($path[1])) { |
| 673 |
$videoId = $path[1]; |
| 674 |
} |
| 675 |
|
| 676 |
} |
| 677 |
|
| 678 |
return '//youtube.com/embed/'.$videoId; |
| 679 |
|
| 680 |
break; |
| 681 |
case 'vimeo': |
| 682 |
|
| 683 |
if (preg_match('/player\.vimeo\.com/is', $url['host']) && preg_match('/video/is', $url['path'])) { |
| 684 |
$path = explode('/', $url['path']); |
| 685 |
|
| 686 |
if (isset($path[2]) && !empty($path[2])) { |
| 687 |
$videoId = $path[2]; |
| 688 |
} |
| 689 |
|
| 690 |
} else if (preg_match('/vimeo\.com/is', $url['host'])) { |
| 691 |
$path = explode('/', $url['path']); |
| 692 |
|
| 693 |
if (isset($path[1]) && !empty($path[1])) { |
| 694 |
$videoId = $path[1]; |
| 695 |
} |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
return '//player.vimeo.com/video/'.$videoId; |
| 700 |
|
| 701 |
break; |
| 702 |
default: |
| 703 |
|
| 704 |
return $source; |
| 705 |
|
| 706 |
} |
| 707 |
|
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
// As per the JS specification |
| 713 |
function pagelayer_escapeHTML($str){ |
| 714 |
|
| 715 |
$replace = [ |
| 716 |
']' => ']', |
| 717 |
'[' => '[', |
| 718 |
//'=' => '=', |
| 719 |
'<' => '<', |
| 720 |
'>' => '>', |
| 721 |
'"' => '"', |
| 722 |
//'&' => '&', |
| 723 |
'\'' => ''' |
| 724 |
]; |
| 725 |
|
| 726 |
$str = str_replace(array_keys($replace), array_values($replace), $str); |
| 727 |
|
| 728 |
return $str; |
| 729 |
} |
| 730 |
|
| 731 |
// As per the JS specification |
| 732 |
function pagelayer_unescapeHTML($str){ |
| 733 |
$replace = [ |
| 734 |
'#93' => ']', |
| 735 |
'#91' => '[', |
| 736 |
//'#61' => '=', |
| 737 |
'lt' => '<', |
| 738 |
'gt' => '>', |
| 739 |
'quot' => '"', |
| 740 |
//'amp' => '&', |
| 741 |
'#39' => '\'' |
| 742 |
]; |
| 743 |
|
| 744 |
foreach($replace as $k => $v){ |
| 745 |
$str = str_replace('&'.$k.';', $v, $str); |
| 746 |
} |
| 747 |
return $str; |
| 748 |
} |