PATH:
home
/
lab2454c
/
foreclass.com
/
wp-content
/
plugins
/
search-replace-for-elementor
/
inc
/
editor
<?php /** * Elementor data formatter. * * @package ELEMSNR * @copyright Copyright (c) 2023, Developry Ltd. * @license https://www.gnu.org/licenses/gpl-2.0.html GNU Public License * @since 1.0 */ namespace ELEMSNR; ! defined( ABSPATH ) || exit; // Exit if accessed directly. if ( ! class_exists( 'Elementor_Data_Formatter' ) ) { class Elementor_Data_Formatter extends \RecursiveIteratorIterator { /** * Search & replace settings. */ public $settings = array(); /** * Keep track of search phrase occurrences. */ public $count_occurrences; /** * Add JSON keywords for Elementor keys to count or search & replace the values. */ public $keys = array(); public function __construct( $iterator, $settings ) { parent::__construct( $iterator, self::SELF_FIRST ); $this->count_occurrences = 0; $this->settings = array( 'search_phrase' => false, 'replace_with_phrase' => false, 'highlight' => false, 'is_text_only' => false, 'is_case_sensitive' => false, 'is_links' => false, 'is_images' => false, 'count' => false, 'regex' => false, ); $this->settings = array_merge( $this->settings, $settings ); // Links only no matter scanned or not. if ( true === $this->settings['is_links'] || true === $this->settings['is_images'] ) { } else { // Add required field keys. $this->keys = json_decode( ELEMSNR_REQUIRED_FIELD_KEYS_ARR ); } $this->keys = array_merge( $this->keys, json_decode( ELEMSNR_DEFAULT_FIELD_KEYS_ARR ) ); } /** * Extend RecursiveIterator `$itr->current()` function to for Search & Replace text (case-sensitive). */ public function current() { $current = parent::current(); // Do HTML regex search & replace. if ( true === $this->settings['regex'] && $this->settings['search_phrase'] ) { // Allow RegEx search & replace only for Text Editor widget. if ( 'editor' === parent::key() ) { $search_phrase = html_entity_decode( $this->settings['search_phrase'] ); $replace_with_phrase = html_entity_decode( $this->settings['replace_with_phrase'] ); preg_replace( "~{$search_phrase}~m", $replace_with_phrase, $current ); if ( preg_last_error() !== PREG_NO_ERROR ) { $elemsnr = new Elementor_Search_Replace(); $elemsnr->print_output_message( 0, '<strong>Regex Error:</strong> ' . $this->preg_last_error_msg() ); } if ( null !== preg_replace( "~{$search_phrase}~m", $replace_with_phrase, $current ) ) { $current = preg_replace( "~{$search_phrase}~m", $replace_with_phrase, $current ); } } } else { foreach ( $this->keys as $keyword ) { if ( parent::key() === $keyword ) { // Count the number of occurrences for the search phrase. if ( true === $this->settings['count'] && $this->settings['search_phrase'] ) { // Allow <a> for links within editor, title, etc. if ( true === $this->settings['is_links'] || true === $this->settings['is_images'] ) { } else { $current_no_tags = $this->strip_tags_with_whitespace( $current ); } if ( true === $this->settings['is_case_sensitive'] ) { } else { preg_match_all( $this->preg_bound( $this->settings['search_phrase'], 'i' ), $current_no_tags, $matches ); $this->count_occurrences += count( $matches[0] ); } } // Do case-sensitive search & replace. if ( $this->settings['search_phrase'] ) { if ( true === $this->settings['is_case_sensitive'] ) { } else { // Overwrite replace with phrase with `current` for highlight we don't // want to change content for case-insensitive search and replace. if ( $this->str_contains( 'elsnr-highlight', $this->settings['replace_with_phrase'] ) ) { // Use trim() to remove trailing white space. $highlight_word = trim( substr( $current, stripos( $current, $this->settings['search_phrase'] ), strlen( $this->settings['search_phrase'] ) ) ); $this->settings['replace_with_phrase'] = "<elsnr-highlight>{$highlight_word}</elsnr-highlight>"; $current = preg_replace( $this->preg_bound( $this->settings['search_phrase'], 'i' ), $this->preg_clean( $this->settings['replace_with_phrase'] ), $current ); } else { $current = str_ireplace( $this->settings['search_phrase'], $this->settings['replace_with_phrase'], $current ); } } } } } } return $current; } /** * Works same as PHP8 `str_contains()` function. */ public function str_contains( $needle, $haystack ) { return '' !== $needle && mb_strpos( $haystack, $needle ) !== false; } /** * Case-insensitive version of the `substr_count()` function. * NOTE: replaced with `preg_match_all` could be removed. */ public function substr_icount( $needle, $haystack ) { return substr_count( strtoupper( $haystack ), strtoupper( $needle ) ); } /** * Escape `preg_replace` or `preg_match` phrase and return as string or * array case-sensitive or insensitive with boundaries. * \b will not work if we have special chars begin/end of the word */ public function preg_bound( $phrase, $case_sensitive = '' ) { return '~(?<!\w)' . preg_quote( stripslashes( $phrase ), '/' ) . "(?!\w)~m{$case_sensitive}"; } /** * Used for I'm, you're etc "Quote content", etc. in search phrases. */ public function preg_clean( $phrase ) { return preg_quote( stripslashes( $phrase ), '/' ); } /** * Extend the `strip_tags()` PHP function to have white spaces between the words. */ public function strip_tags_with_whitespace( $html, $allowable_tags = null ) { $html = str_replace( '<', ' <', $html ); $html = strip_tags( $html, $allowable_tags ); $html = preg_replace( '/\s\s+/', ' ', $html ); $html = trim( $html ); return $html; } /** * Works same as PHP8 `preg_last_error_msg` function. */ public function preg_last_error_msg() { switch ( preg_last_error() ) { case PREG_NO_ERROR: return __( 'No error!', 'search-replace-for-elementor' ); case PREG_INTERNAL_ERROR: return __( 'Internal error!', 'search-replace-for-elementor' ); case PREG_BACKTRACK_LIMIT_ERROR: return __( 'Backtrack limit exhausted!', 'search-replace-for-elementor' ); case PREG_RECURSION_LIMIT_ERROR: return __( 'Recursion limit exhausted!', 'search-replace-for-elementor' ); case PREG_BAD_UTF8_ERROR: return __( 'Malformed UTF-8 characters, possibly incorrectly encoded!', 'search-replace-for-elementor' ); case PREG_BAD_UTF8_OFFSET_ERROR: return __( 'The offset did not correspond to the beginning of a valid UTF-8 code point!', 'search-replace-for-elementor' ); case PREG_JIT_STACKLIMIT_ERROR: return __( 'JIT stack limit exhausted!', 'search-replace-for-elementor' ); default: return __( 'Unknown error!', 'search-replace-for-elementor' ); } } } }
[+]
..
[-] class-elementor-data-formatter.php
[edit]
[-] class-elementor-search-replace.php
[edit]