| 1 |
<?php |
| 2 |
|
| 3 |
namespace KaizenCoders\UpdateURLS; |
| 4 |
|
| 5 |
use KaizenCoders\UpdateURLS\Option; |
| 6 |
|
| 7 |
/** |
| 8 |
* Plugin_Name |
| 9 |
* |
| 10 |
* @link https://kaizencoders.com |
| 11 |
* @author KaizenCoders <hello@kaizencoders.com> |
| 12 |
* @package UpdateURLS |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Helper Class |
| 17 |
*/ |
| 18 |
class Helper { |
| 19 |
|
| 20 |
/** |
| 21 |
* Whether given user is an administrator. |
| 22 |
* |
| 23 |
* @param \WP_User $user The given user. |
| 24 |
* |
| 25 |
* @return bool |
| 26 |
*/ |
| 27 |
public static function is_user_admin( \WP_User $user = null ) { |
| 28 |
if ( is_null( $user ) ) { |
| 29 |
$user = wp_get_current_user(); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! $user instanceof WP_User ) { |
| 33 |
_doing_it_wrong( __METHOD__, 'To check if the user is admin is required a WP_User object.', '1.0.0' ); |
| 34 |
} |
| 35 |
|
| 36 |
return is_multisite() ? user_can( $user, 'manage_network' ) : user_can( $user, 'manage_options' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* What type of request is this? |
| 41 |
* |
| 42 |
* @param string $type admin, ajax, cron, cli or frontend. |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
* @since 1.2 |
| 46 |
* |
| 47 |
*/ |
| 48 |
public function request( $type ) { |
| 49 |
switch ( $type ) { |
| 50 |
case 'admin_backend': |
| 51 |
return $this->is_admin_backend(); |
| 52 |
case 'ajax': |
| 53 |
return $this->is_ajax(); |
| 54 |
case 'installing_wp': |
| 55 |
return $this->is_installing_wp(); |
| 56 |
case 'rest': |
| 57 |
return $this->is_rest(); |
| 58 |
case 'cron': |
| 59 |
return $this->is_cron(); |
| 60 |
case 'frontend': |
| 61 |
return $this->is_frontend(); |
| 62 |
case 'cli': |
| 63 |
return $this->is_cli(); |
| 64 |
default: |
| 65 |
_doing_it_wrong( __METHOD__, esc_html( sprintf( 'Unknown request type: %s', $type ) ), '1.0.0' ); |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Is installing WP |
| 73 |
* |
| 74 |
* @return boolean |
| 75 |
*/ |
| 76 |
public function is_installing_wp() { |
| 77 |
return defined( 'WP_INSTALLING' ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Is admin |
| 82 |
* |
| 83 |
* @return boolean |
| 84 |
* @since 1.2 |
| 85 |
* |
| 86 |
*/ |
| 87 |
public function is_admin_backend() { |
| 88 |
return is_user_logged_in() && is_admin(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Is ajax |
| 93 |
* |
| 94 |
* @return boolean |
| 95 |
* @since 1.2 |
| 96 |
* |
| 97 |
*/ |
| 98 |
public function is_ajax() { |
| 99 |
return ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) || defined( 'DOING_AJAX' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Is rest |
| 104 |
* |
| 105 |
* @return boolean |
| 106 |
* @since 1.2 |
| 107 |
* |
| 108 |
*/ |
| 109 |
public function is_rest() { |
| 110 |
return defined( 'REST_REQUEST' ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Is cron |
| 115 |
* |
| 116 |
* @return boolean |
| 117 |
* @since 1.2 |
| 118 |
* |
| 119 |
*/ |
| 120 |
public function is_cron() { |
| 121 |
return ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() ) || defined( 'DOING_CRON' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Is frontend |
| 126 |
* |
| 127 |
* @return boolean |
| 128 |
* @since 1.2 |
| 129 |
* |
| 130 |
*/ |
| 131 |
public function is_frontend() { |
| 132 |
return ( ! $this->is_admin_backend() || ! $this->is_ajax() ) && ! $this->is_cron() && ! $this->is_rest(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Is cli |
| 137 |
* |
| 138 |
* @return boolean |
| 139 |
* @since 1.2 |
| 140 |
* |
| 141 |
*/ |
| 142 |
public function is_cli() { |
| 143 |
return defined( 'WP_CLI' ) && WP_CLI; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Define constant |
| 148 |
* |
| 149 |
* @param $name |
| 150 |
* @param $value |
| 151 |
* |
| 152 |
* @since 1.2 |
| 153 |
*/ |
| 154 |
public static function maybe_define_constant( $name, $value ) { |
| 155 |
if ( ! defined( $name ) ) { |
| 156 |
define( $name, $value ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get current date time |
| 162 |
* |
| 163 |
* @return false|string |
| 164 |
*/ |
| 165 |
public static function get_current_date_time() { |
| 166 |
return gmdate( 'Y-m-d H:i:s' ); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Get current date time |
| 172 |
* |
| 173 |
* @return false|string |
| 174 |
* |
| 175 |
*/ |
| 176 |
public static function get_current_gmt_timestamp() { |
| 177 |
return strtotime( gmdate( 'Y-m-d H:i:s' ) ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get current date |
| 182 |
* |
| 183 |
* @return false|string |
| 184 |
* |
| 185 |
*/ |
| 186 |
public static function get_current_date() { |
| 187 |
return gmdate( 'Y-m-d' ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Format date time |
| 192 |
* |
| 193 |
* @param $date |
| 194 |
* |
| 195 |
* @return string |
| 196 |
* |
| 197 |
* @since 1.2 |
| 198 |
*/ |
| 199 |
public static function format_date_time( $date ) { |
| 200 |
$convert_date_format = get_option( 'date_format' ); |
| 201 |
$convert_time_format = get_option( 'time_format' ); |
| 202 |
|
| 203 |
$local_timestamp = ( $date !== '0000-00-00 00:00:00' ) ? date_i18n( "$convert_date_format $convert_time_format", strtotime( get_date_from_gmt( $date ) ) ) : '<i class="dashicons dashicons-es dashicons-minus"></i>'; |
| 204 |
|
| 205 |
return $local_timestamp; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Clean String or array using sanitize_text_field |
| 210 |
* |
| 211 |
* @param $variable Data to sanitize |
| 212 |
* |
| 213 |
* @return array|string |
| 214 |
* |
| 215 |
* @since 1.2 |
| 216 |
*/ |
| 217 |
public static function clean( $var ) { |
| 218 |
if ( is_array( $var ) ) { |
| 219 |
return array_map( 'clean', $var ); |
| 220 |
} else { |
| 221 |
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get IP |
| 227 |
* |
| 228 |
* @return mixed|string|void |
| 229 |
* |
| 230 |
* @since 1.2 |
| 231 |
*/ |
| 232 |
public static function get_ip() { |
| 233 |
// Get real visitor IP behind CloudFlare network |
| 234 |
if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) { |
| 235 |
$ip = $_SERVER['HTTP_CF_CONNECTING_IP']; |
| 236 |
} elseif ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { |
| 237 |
$ip = $_SERVER['HTTP_X_REAL_IP']; |
| 238 |
} elseif ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 239 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 240 |
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 241 |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 242 |
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) { |
| 243 |
$ip = $_SERVER['HTTP_X_FORWARDED']; |
| 244 |
} elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) { |
| 245 |
$ip = $_SERVER['HTTP_FORWARDED_FOR']; |
| 246 |
} elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) { |
| 247 |
$ip = $_SERVER['HTTP_FORWARDED']; |
| 248 |
} else { |
| 249 |
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : 'UNKNOWN'; |
| 250 |
} |
| 251 |
|
| 252 |
return $ip; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Get GMT Offset |
| 257 |
* |
| 258 |
* @param bool $in_seconds |
| 259 |
* @param null $timestamp |
| 260 |
* |
| 261 |
* @return float|int |
| 262 |
* |
| 263 |
* @since 1.2 |
| 264 |
*/ |
| 265 |
public static function get_gmt_offset( $in_seconds = false, $timestamp = null ) { |
| 266 |
$offset = get_option( 'gmt_offset' ); |
| 267 |
|
| 268 |
if ( $offset == '' ) { |
| 269 |
$tzstring = get_option( 'timezone_string' ); |
| 270 |
$current = date_default_timezone_get(); |
| 271 |
date_default_timezone_set( $tzstring ); |
| 272 |
$offset = date( 'Z' ) / 3600; |
| 273 |
date_default_timezone_set( $current ); |
| 274 |
} |
| 275 |
|
| 276 |
// check if timestamp has DST |
| 277 |
if ( ! is_null( $timestamp ) ) { |
| 278 |
$l = localtime( $timestamp, true ); |
| 279 |
if ( $l['tm_isdst'] ) { |
| 280 |
$offset ++; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return $in_seconds ? $offset * 3600 : (int) $offset; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Insert $new in $array after $key |
| 289 |
* |
| 290 |
* @param $array |
| 291 |
* @param $key |
| 292 |
* @param $new |
| 293 |
* |
| 294 |
* @return array |
| 295 |
* |
| 296 |
* @since 1.2 |
| 297 |
*/ |
| 298 |
public static function array_insert_after( $array, $key, $new ) { |
| 299 |
$keys = array_keys( $array ); |
| 300 |
$index = array_search( $key, $keys ); |
| 301 |
$pos = false === $index ? count( $array ) : $index + 1; |
| 302 |
|
| 303 |
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Insert a value or key/value pair before a specific key in an array. If key doesn't exist, value is prepended |
| 308 |
* to the beginning of the array. |
| 309 |
* |
| 310 |
* @param array $array |
| 311 |
* @param string $key |
| 312 |
* @param array $new |
| 313 |
* |
| 314 |
* @return array |
| 315 |
* |
| 316 |
* @since 1.2 |
| 317 |
*/ |
| 318 |
public static function array_insert_before( array $array, $key, array $new ) { |
| 319 |
$keys = array_keys( $array ); |
| 320 |
$pos = (int) array_search( $key, $keys ); |
| 321 |
|
| 322 |
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) ); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Insert $new in $array after $key |
| 327 |
* |
| 328 |
* @param $array |
| 329 |
* |
| 330 |
* @return boolean |
| 331 |
* |
| 332 |
* @since 1.2 |
| 333 |
*/ |
| 334 |
public static function is_forechable( $array = [] ) { |
| 335 |
if ( ! is_array( $array ) ) { |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
if ( empty( $array ) ) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
if ( count( $array ) <= 0 ) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
return true; |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Get current db version |
| 353 |
* |
| 354 |
* @since 1.2 |
| 355 |
*/ |
| 356 |
public static function get_db_version() { |
| 357 |
return Option::get( 'db_version', null ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Get data from array |
| 362 |
* |
| 363 |
* @param array $array |
| 364 |
* @param string $var |
| 365 |
* @param string $default |
| 366 |
* @param bool $clean |
| 367 |
* |
| 368 |
* @return array|string |
| 369 |
* |
| 370 |
* @since 1.2 |
| 371 |
*/ |
| 372 |
public static function get_data( $array = [], $var = '', $default = '', $clean = false ) { |
| 373 |
if ( empty( $array ) ) { |
| 374 |
return $default; |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! empty( $var ) || ( 0 === $var ) ) { |
| 378 |
if ( strpos( $var, '|' ) > 0 ) { |
| 379 |
$vars = array_map( 'trim', explode( '|', $var ) ); |
| 380 |
foreach ( $vars as $var ) { |
| 381 |
if ( isset( $array[ $var ] ) ) { |
| 382 |
$array = $array[ $var ]; |
| 383 |
} else { |
| 384 |
return $default; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
return wp_unslash( $array ); |
| 389 |
} else { |
| 390 |
$value = isset( $array[ $var ] ) ? wp_unslash( $array[ $var ] ) : $default; |
| 391 |
} |
| 392 |
} else { |
| 393 |
$value = wp_unslash( $array ); |
| 394 |
} |
| 395 |
|
| 396 |
if ( $clean ) { |
| 397 |
$value = self::clean( $value ); |
| 398 |
} |
| 399 |
|
| 400 |
return $value; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/** |
| 405 |
* Get POST | GET data from $_REQUEST |
| 406 |
* |
| 407 |
* @param string $var |
| 408 |
* @param string $default |
| 409 |
* @param bool $clean |
| 410 |
* |
| 411 |
* @return array|string |
| 412 |
* |
| 413 |
* @since 1.2 |
| 414 |
*/ |
| 415 |
public static function get_request_data( $var = '', $default = '', $clean = true ) { |
| 416 |
return self::get_data( $_REQUEST, $var, $default, $clean ); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Get POST data from $_POST |
| 421 |
* |
| 422 |
* @param string $var |
| 423 |
* @param string $default |
| 424 |
* @param bool $clean |
| 425 |
* |
| 426 |
* @return array|string |
| 427 |
* |
| 428 |
* @since 1.2 |
| 429 |
*/ |
| 430 |
public static function get_post_data( $var = '', $default = '', $clean = true ) { |
| 431 |
return self::get_data( $_POST, $var, $default, $clean ); |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Get Current Screen Id |
| 436 |
* |
| 437 |
* @return string |
| 438 |
* |
| 439 |
* @since 1.2 |
| 440 |
*/ |
| 441 |
public static function get_current_screen_id() { |
| 442 |
$current_screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; |
| 443 |
|
| 444 |
if ( ! $current_screen instanceof \WP_Screen ) { |
| 445 |
return ''; |
| 446 |
} |
| 447 |
|
| 448 |
$current_screen = get_current_screen(); |
| 449 |
|
| 450 |
return ( $current_screen ? $current_screen->id : '' ); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Append UTM parameters to a url. |
| 455 |
* |
| 456 |
* Keys may be given with or without the `utm_` prefix. Existing query |
| 457 |
* arguments are preserved and a fragment stays at the end where it belongs, |
| 458 |
* so a url such as `.../update-urls/#pricing` keeps working. |
| 459 |
* |
| 460 |
* @param string $url Target url. |
| 461 |
* @param array $params Parameters, e.g. [ 'source' => 'x', 'medium' => 'y' ]. |
| 462 |
* |
| 463 |
* @return string |
| 464 |
* |
| 465 |
* @since 1.5.0 |
| 466 |
*/ |
| 467 |
public static function add_utm_params( $url, $params ) { |
| 468 |
if ( '' === (string) $url || empty( $params ) ) { |
| 469 |
return (string) $url; |
| 470 |
} |
| 471 |
|
| 472 |
$utm = []; |
| 473 |
|
| 474 |
foreach ( (array) $params as $key => $value ) { |
| 475 |
$value = (string) $value; |
| 476 |
|
| 477 |
if ( '' === $value ) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
$key = (string) $key; |
| 482 |
|
| 483 |
if ( 0 !== strpos( $key, 'utm_' ) ) { |
| 484 |
$key = 'utm_' . $key; |
| 485 |
} |
| 486 |
|
| 487 |
$utm[ $key ] = $value; |
| 488 |
} |
| 489 |
|
| 490 |
if ( empty( $utm ) ) { |
| 491 |
return (string) $url; |
| 492 |
} |
| 493 |
|
| 494 |
$parts = wp_parse_url( $url ); |
| 495 |
|
| 496 |
$scheme = isset( $parts['scheme'] ) ? $parts['scheme'] . '://' : ''; |
| 497 |
$user = isset( $parts['user'] ) ? $parts['user'] . ( isset( $parts['pass'] ) ? ':' . $parts['pass'] : '' ) . '@' : ''; |
| 498 |
$host = isset( $parts['host'] ) ? $parts['host'] : ''; |
| 499 |
$port = isset( $parts['port'] ) ? ':' . $parts['port'] : ''; |
| 500 |
$path = isset( $parts['path'] ) ? $parts['path'] : ''; |
| 501 |
$query = isset( $parts['query'] ) ? $parts['query'] : ''; |
| 502 |
$fragment = isset( $parts['fragment'] ) ? '#' . $parts['fragment'] : ''; |
| 503 |
|
| 504 |
// `https://example.com` has no path, which would render as |
| 505 |
// `https://example.com?utm_source=…`. Valid, but the slash belongs there. |
| 506 |
if ( '' === $path && '' !== $host ) { |
| 507 |
$path = '/'; |
| 508 |
} |
| 509 |
|
| 510 |
$existing = []; |
| 511 |
|
| 512 |
if ( '' !== $query ) { |
| 513 |
parse_str( $query, $existing ); |
| 514 |
} |
| 515 |
|
| 516 |
$merged = array_merge( is_array( $existing ) ? $existing : [], $utm ); |
| 517 |
|
| 518 |
$rebuilt_query = http_build_query( $merged ); |
| 519 |
|
| 520 |
return $scheme . $user . $host . $port . $path |
| 521 |
. ( '' !== $rebuilt_query ? '?' . $rebuilt_query : '' ) |
| 522 |
. $fragment; |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* Tag an outbound url with the plugin's standard campaign parameters. |
| 527 |
* |
| 528 |
* Single place where the UTM convention lives, so every link that leaves |
| 529 |
* the plugin for kaizencoders.com is attributed the same way. |
| 530 |
* |
| 531 |
* @param string $url Target url. |
| 532 |
* @param array $args { |
| 533 |
* @type string $source Defaults to `update-urls-in-app`. |
| 534 |
* @type string $medium Where the click came from, e.g. `banner`, `email`. |
| 535 |
* @type string $campaign Campaign identifier. |
| 536 |
* @type string $content Optional placement within the medium. |
| 537 |
* } |
| 538 |
* |
| 539 |
* @return string |
| 540 |
* |
| 541 |
* @since 1.5.0 |
| 542 |
*/ |
| 543 |
public static function get_utm_url( $url, $args = [] ) { |
| 544 |
$args = wp_parse_args( |
| 545 |
$args, |
| 546 |
[ |
| 547 |
'source' => 'update-urls-in-app', |
| 548 |
'medium' => '', |
| 549 |
'campaign' => '', |
| 550 |
'content' => '', |
| 551 |
] |
| 552 |
); |
| 553 |
|
| 554 |
/** |
| 555 |
* Filter the UTM parameters applied to outbound plugin links. |
| 556 |
* |
| 557 |
* @param array $args UTM parameters. |
| 558 |
* @param string $url Target url. |
| 559 |
* |
| 560 |
* @since 1.5.0 |
| 561 |
*/ |
| 562 |
$args = apply_filters( 'kc_uu_utm_params', $args, $url ); |
| 563 |
|
| 564 |
return self::add_utm_params( $url, $args ); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Get all Plugin admin screens |
| 569 |
* |
| 570 |
* @return array|mixed|void |
| 571 |
* |
| 572 |
* @since 1.2 |
| 573 |
*/ |
| 574 |
public static function get_plugin_admin_screens() { |
| 575 |
$prefix = sanitize_title( __( 'Update URLS', 'update-urls' ) ); |
| 576 |
|
| 577 |
$screens = [ |
| 578 |
"toplevel_page_update-urls", |
| 579 |
"{$prefix}_page_update-urls-history", |
| 580 |
"{$prefix}_page_kc-uu-settings", |
| 581 |
"{$prefix}_page_update-urls-tools", |
| 582 |
"{$prefix}_page_update-urls-other-products", |
| 583 |
]; |
| 584 |
|
| 585 |
$screens = apply_filters( 'kc_uu_admin_screens', $screens ); |
| 586 |
|
| 587 |
return $screens; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Is es admin screen? |
| 592 |
* |
| 593 |
* @param string $screen_id Admin screen id |
| 594 |
* |
| 595 |
* @return bool |
| 596 |
* |
| 597 |
* @since 1.0.0 |
| 598 |
*/ |
| 599 |
public static function is_plugin_admin_screen( $screen_id = '' ) { |
| 600 |
$current_screen_id = self::get_current_screen_id(); |
| 601 |
|
| 602 |
// Check for specific admin screen id if passed. |
| 603 |
if ( ! empty( $screen_id ) ) { |
| 604 |
if ( $current_screen_id === $screen_id ) { |
| 605 |
return true; |
| 606 |
} else { |
| 607 |
return false; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
$plugin_admin_screens = self::get_plugin_admin_screens(); |
| 612 |
|
| 613 |
if ( in_array( $current_screen_id, $plugin_admin_screens ) ) { |
| 614 |
return true; |
| 615 |
} |
| 616 |
|
| 617 |
return false; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Replace into serialised data. |
| 622 |
* |
| 623 |
* @param $from |
| 624 |
* @param $to |
| 625 |
* @param $data |
| 626 |
* @param $serialised |
| 627 |
* |
| 628 |
* @return array|mixed|object|string|string[] |
| 629 |
* |
| 630 |
* @since 1.2 |
| 631 |
*/ |
| 632 |
public static function replace_into_serialized_data( $from = '', $to = '', $data = '', $serialised = false ) { |
| 633 |
try { |
| 634 |
if ( false !== is_serialized( $data ) ) { |
| 635 |
$un_serialized = maybe_unserialize( $data ); |
| 636 |
$data = self::replace_into_serialized_data( $from, $to, $un_serialized, true ); |
| 637 |
} elseif ( is_array( $data ) ) { |
| 638 |
$_tmp = []; |
| 639 |
foreach ( $data as $key => $value ) { |
| 640 |
$_tmp[ $key ] = self::replace_into_serialized_data( $from, $to, $value, false ); |
| 641 |
} |
| 642 |
$data = $_tmp; |
| 643 |
unset( $_tmp ); |
| 644 |
} else { |
| 645 |
if ( is_string( $data ) ) { |
| 646 |
$data = str_replace( $from, $to, $data ); |
| 647 |
} |
| 648 |
} |
| 649 |
if ( $serialised ) { |
| 650 |
return maybe_serialize( $data ); |
| 651 |
} |
| 652 |
} catch ( Exception $error ) { |
| 653 |
} |
| 654 |
|
| 655 |
return $data; |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
* Update URLS |
| 660 |
* |
| 661 |
* @param $options |
| 662 |
* @param $oldurl |
| 663 |
* @param $newurl |
| 664 |
* |
| 665 |
* @return array |
| 666 |
* |
| 667 |
* @since 1.2 |
| 668 |
*/ |
| 669 |
public static function UpdateURLS( $options, $oldurl, $newurl ) { |
| 670 |
global $wpdb; |
| 671 |
|
| 672 |
$results = []; |
| 673 |
$queries = [ |
| 674 |
'content' => [ |
| 675 |
"UPDATE $wpdb->posts SET post_content = replace(post_content, %s, %s)", |
| 676 |
__( 'Content Items (Posts, Pages, Custom Post Types, Revisions)', 'update-urls' ), |
| 677 |
], |
| 678 |
'excerpts' => [ |
| 679 |
"UPDATE $wpdb->posts SET post_excerpt = replace(post_excerpt, %s, %s)", |
| 680 |
__( 'Excerpts', 'update-urls' ), |
| 681 |
], |
| 682 |
'attachments' => [ |
| 683 |
"UPDATE $wpdb->posts SET guid = replace(guid, %s, %s) WHERE post_type = 'attachment'", |
| 684 |
__( 'Attachments', 'update-urls' ), |
| 685 |
], |
| 686 |
'links' => [ |
| 687 |
"UPDATE $wpdb->links SET link_url = replace(link_url, %s, %s)", |
| 688 |
__( 'Links', 'update-urls' ), |
| 689 |
], |
| 690 |
'custom' => [ |
| 691 |
"UPDATE $wpdb->postmeta SET meta_value = replace(meta_value, %s, %s)", |
| 692 |
__( 'Custom Fields', 'update-urls' ), |
| 693 |
], |
| 694 |
'guids' => [ |
| 695 |
"UPDATE $wpdb->posts SET guid = replace(guid, %s, %s)", |
| 696 |
__( 'GUIDs', 'update-urls' ), |
| 697 |
], |
| 698 |
]; |
| 699 |
|
| 700 |
foreach ( $options as $option ) { |
| 701 |
if ( 'custom' === $option ) { |
| 702 |
$n = 0; |
| 703 |
$row_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->postmeta" ); |
| 704 |
$page_size = 10000; |
| 705 |
$pages = ceil( $row_count / $page_size ); |
| 706 |
|
| 707 |
for ( $page = 0; $page < $pages; $page ++ ) { |
| 708 |
$start = $page * $page_size; |
| 709 |
$pmquery = "SELECT * FROM $wpdb->postmeta WHERE meta_value <> '' LIMIT $page_size OFFSET $start"; |
| 710 |
$items = $wpdb->get_results( $pmquery ); |
| 711 |
foreach ( $items as $item ) { |
| 712 |
$value = $item->meta_value; |
| 713 |
if ( trim( $value ) == '' ) { |
| 714 |
continue; |
| 715 |
} |
| 716 |
|
| 717 |
$edited = self::replace_into_serialized_data( $oldurl, $newurl, $value ); |
| 718 |
|
| 719 |
if ( $edited != $value ) { |
| 720 |
$fix = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->postmeta SET meta_value = %s WHERE meta_id = %d", $edited, $item->meta_id ) ); |
| 721 |
if ( $fix ) { |
| 722 |
$n ++; |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
$results[ $option ] = [ $n, $queries[ $option ][1] ]; |
| 728 |
} else { |
| 729 |
$result = $wpdb->query( $wpdb->prepare( $queries[ $option ][0], $oldurl, $newurl ) ); |
| 730 |
$results[ $option ] = [ $result, $queries[ $option ][1] ]; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
return $results; |
| 735 |
} |
| 736 |
|
| 737 |
public static function can_show_promotion( $conditions = [], $force = false ) { |
| 738 |
if ( ! Helper::is_plugin_admin_screen() ) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
|
| 742 |
if ( $force ) { |
| 743 |
return true; |
| 744 |
} |
| 745 |
|
| 746 |
$conditions = array_merge( |
| 747 |
[ |
| 748 |
'check_plan' => 'pro', |
| 749 |
'meta' => [], |
| 750 |
'start_after_installation_days' => 2, |
| 751 |
'end_before_installation_days' => 999999, |
| 752 |
'total_links' => 2, |
| 753 |
'start_date' => null, |
| 754 |
'end_date' => null, |
| 755 |
'promotion' => null, |
| 756 |
], $conditions |
| 757 |
); |
| 758 |
|
| 759 |
extract( $conditions ); |
| 760 |
|
| 761 |
if ( $check_plan == 'free' ) { |
| 762 |
if ( UU()->is_pro() ) { |
| 763 |
return false; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
// Already seen this promotion? |
| 768 |
if ( ! is_null( $promotion ) && self::is_promotion_dismissed( $promotion ) ) { |
| 769 |
return false; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
$today = Helper::get_current_date_time(); |
| 775 |
|
| 776 |
// Don't show if start date is future. |
| 777 |
if ( ! is_null( $start_date ) && ( $today < $start_date ) ) { |
| 778 |
return false; |
| 779 |
} |
| 780 |
|
| 781 |
// Don't show if end date is past. |
| 782 |
if ( ! is_null( $end_date ) && ( $today > $end_date ) ) { |
| 783 |
return false; |
| 784 |
} |
| 785 |
|
| 786 |
$installed_on = Option::get( 'installed_on', 0 ); |
| 787 |
if ( 0 === $installed_on ) { |
| 788 |
Option::set( 'installed_on', time() ); |
| 789 |
} |
| 790 |
|
| 791 |
$since_installed = ceil( ( time() - $installed_on ) / 86400 ); |
| 792 |
|
| 793 |
if ( $since_installed >= $start_after_installation_days && $since_installed <= $end_before_installation_days ) { |
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
return false; |
| 798 |
} |
| 799 |
|
| 800 |
public static function get_upgrade_banner( $query_strings = [], $show_coupon = false, $data = [] ) { |
| 801 |
$message = Helper::get_data( $data, 'message', '' ); |
| 802 |
$title = Helper::get_data( $data, 'title', 'Upgrade Now.' ); |
| 803 |
$coupon_message = Helper::get_data( $data, 'coupon_message', '' ); |
| 804 |
$pricing_url = Helper::get_data( $data, 'pricing_url', '' ); |
| 805 |
$dismiss_url = Helper::get_data( $data, 'dismiss_url', '' ); |
| 806 |
$show_upgrade = Helper::get_data( $data, 'show_upgrade', true ); |
| 807 |
|
| 808 |
if ( $query_strings ) { |
| 809 |
$pricing_url = add_query_arg( $query_strings, $pricing_url ); |
| 810 |
$dismiss_url = add_query_arg( $query_strings, $dismiss_url ); |
| 811 |
} |
| 812 |
|
| 813 |
?> |
| 814 |
|
| 815 |
<div class="rounded-md bg-green-50 p-4"> |
| 816 |
<div class="flex"> |
| 817 |
<div class="flex-shrink-0"> |
| 818 |
<svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
| 819 |
<path fill-rule="evenodd" |
| 820 |
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z" |
| 821 |
clip-rule="evenodd"/> |
| 822 |
</svg> |
| 823 |
</div> |
| 824 |
<div class="ml-3"> |
| 825 |
<h3 class="text-sm font-medium text-green-800"><?php |
| 826 |
echo $title; ?></h3> |
| 827 |
<div class="mt-2 text-sm"> |
| 828 |
<span class="text-base"> |
| 829 |
<?php |
| 830 |
echo $message; ?> |
| 831 |
|
| 832 |
<?php |
| 833 |
if ( $show_coupon ) { ?> |
| 834 |
<br/> |
| 835 |
<?php |
| 836 |
echo $coupon_message; |
| 837 |
} ?> |
| 838 |
</span> |
| 839 |
</div> |
| 840 |
<div class="mt-4"> |
| 841 |
<div class="-mx-2 -my-1.5 flex"> |
| 842 |
<?php |
| 843 |
if ( $show_upgrade ) { ?> |
| 844 |
<button type="button" |
| 845 |
class="rounded-md border-2 border-green-800 bg-green-50 px-2 py-1.5 text-sm font-medium text-green-800 hover:bg-green-100 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-offset-2 focus:ring-offset-green-50"> |
| 846 |
<a href="<?php |
| 847 |
echo esc_url( $pricing_url ); ?>" class="text-green-800 hover:text-green-800" |
| 848 |
target="_blank">Upgrade |
| 849 |
Now</a></button> |
| 850 |
<?php |
| 851 |
} ?> |
| 852 |
<button type="button" |
| 853 |
class="ml-3 rounded-md px-2 py-1.5 text-sm font-medium text-red-800 focus:outline-none focus:ring-2"> |
| 854 |
<a href="<?php |
| 855 |
echo esc_url( $dismiss_url ); ?>" class="text-red-500">Dismiss</a> |
| 856 |
</button> |
| 857 |
</div> |
| 858 |
</div> |
| 859 |
</div> |
| 860 |
</div> |
| 861 |
</div> |
| 862 |
<?php |
| 863 |
} |
| 864 |
|
| 865 |
public static function is_promotion_dismissed( $promotion ) { |
| 866 |
if ( empty( $promotion ) ) { |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
$promotion_dismissed_option = 'kc_uu_' . trim( $promotion ) . '_dismissed'; |
| 871 |
|
| 872 |
return 'yes' === get_option( $promotion_dismissed_option ); |
| 873 |
} |
| 874 |
|
| 875 |
public static function get_kc_plugins_info( $force = false ) { |
| 876 |
// Get cached data |
| 877 |
$plugins_info = get_transient( 'kc_plugins_info' ); |
| 878 |
|
| 879 |
if ( $force || false === $plugins_info ) { |
| 880 |
// Base plugin data |
| 881 |
$plugins = [ |
| 882 |
'url-shortify' => [ |
| 883 |
'name' => 'url-shortify/url-shortify.php', |
| 884 |
'is_premium' => true, |
| 885 |
'premium_slug' => 'url-shortify-premium/url-shortify.php', |
| 886 |
'premium_url' => 'https://kaizencoders.com/url-shortify', |
| 887 |
], |
| 888 |
|
| 889 |
'update-urls' => [ |
| 890 |
'name' => 'update-urls/update-urls.php', |
| 891 |
'is_premium' => true, |
| 892 |
'premium_slug' => 'update-urls-premium/update-urls.php', |
| 893 |
'premium_url' => 'https://kaizencoders.com/update-urls', |
| 894 |
|
| 895 |
], |
| 896 |
|
| 897 |
'logify' => [ |
| 898 |
'name' => 'logify/logify.php', |
| 899 |
'is_premium' => false, |
| 900 |
], |
| 901 |
|
| 902 |
'magic-link' => [ |
| 903 |
'name' => 'magic-link/magic-link.php', |
| 904 |
'is_premium' => false, |
| 905 |
], |
| 906 |
|
| 907 |
|
| 908 |
'social-linkz' => [ |
| 909 |
'name' => 'social-linkz/social-linkz.php', |
| 910 |
'is_premium' => false, |
| 911 |
], |
| 912 |
|
| 913 |
'zapify' => [ |
| 914 |
'name' => 'zapify/zapify.php', |
| 915 |
'is_premium' => false, |
| 916 |
], |
| 917 |
'utilitify' => [ |
| 918 |
'name' => 'utilitify/utilitify.php', |
| 919 |
'is_premium' => false, |
| 920 |
], |
| 921 |
]; |
| 922 |
|
| 923 |
$plugins_info = []; |
| 924 |
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
| 925 |
|
| 926 |
foreach ( $plugins as $slug => $base_data ) { |
| 927 |
$api = plugins_api( 'plugin_information', [ |
| 928 |
'slug' => $slug, |
| 929 |
'fields' => [ |
| 930 |
'short_description' => true, |
| 931 |
'icons' => true, |
| 932 |
], |
| 933 |
] ); |
| 934 |
|
| 935 |
if ( ! is_wp_error( $api ) ) { |
| 936 |
$data = [ |
| 937 |
'title' => $api->name, |
| 938 |
'logo' => isset( $api->icons['2x'] ) ? $api->icons['2x'] : ( isset( $api->icons['1x'] ) ? $api->icons['1x'] : ( isset( $api->icons['default'] ) ? $api->icons['default'] : '' ) ), |
| 939 |
'desc' => $api->short_description, |
| 940 |
'plugin_url' => "https://wordpress.org/plugins/{$slug}/", |
| 941 |
'slug' => $slug, |
| 942 |
]; |
| 943 |
|
| 944 |
$plugins_info[ $slug ] = array_merge( $data, $base_data ); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
// Cache for 30 days |
| 949 |
set_transient( 'kc_plugins_info', $plugins_info, 7 * DAY_IN_SECONDS ); |
| 950 |
} |
| 951 |
|
| 952 |
return $plugins_info; |
| 953 |
} |
| 954 |
} |