admin
2 months ago
register-hooks
3 years ago
class-wpel-front-ignore.php
3 years ago
class-wpel-front.php
4 months ago
class-wpel-link.php
3 years ago
class-wpel-plugin.php
1 year ago
class-wpel-register-scripts.php
2 months ago
class-wpel-template-tags.php
3 years ago
class-wpel-update.php
3 years ago
index.php
6 years ago
class-wpel-front-ignore.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPEL_Front_Ignore |
| 4 | * |
| 5 | * @package WPEL |
| 6 | * @category WordPress Plugin |
| 7 | * @version 2.3 |
| 8 | * @link https://www.webfactoryltd.com/ |
| 9 | * @license Dual licensed under the MIT and GPLv2+ licenses |
| 10 | */ |
| 11 | final class WPEL_Front_Ignore extends WPRun_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $content_placeholders = array(); |
| 18 | |
| 19 | /** |
| 20 | * @var WPEL_Settings_Page |
| 21 | */ |
| 22 | private $settings_page = null; |
| 23 | |
| 24 | /** |
| 25 | * Initialize |
| 26 | * @param WPEL_Settings_Page $settings_page |
| 27 | */ |
| 28 | protected function init( WPEL_Settings_Page $settings_page ) |
| 29 | { |
| 30 | $this->settings_page = $settings_page; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get option value |
| 35 | * @param string $key |
| 36 | * @param string|null $type |
| 37 | * @return string |
| 38 | * @triggers E_USER_NOTICE Option value cannot be found |
| 39 | */ |
| 40 | protected function opt( $key, $type = null ) |
| 41 | { |
| 42 | return $this->settings_page->get_option_value( $key, $type ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Skip complete pages |
| 47 | * @return boolean |
| 48 | */ |
| 49 | protected function filter_wpel_apply_settings() |
| 50 | { |
| 51 | if ( ! is_single() && ! is_page() ) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | $current_post_id = get_queried_object_id(); |
| 56 | $skip_post_ids = $this->opt( 'skip_post_ids', 'exceptions' ); |
| 57 | $skip_post_ids_arr = explode( ',', $skip_post_ids ); |
| 58 | |
| 59 | foreach ( $skip_post_ids_arr as $post_id ) { |
| 60 | if ( intval( $post_id ) === $current_post_id ) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Action for "wpel_before_apply_link" |
| 70 | * @param WPEL_Link $link |
| 71 | */ |
| 72 | protected function action_wpel_before_apply_link_10000000000( WPEL_Link $link ) |
| 73 | { |
| 74 | // ignore mailto links |
| 75 | if ( $this->opt( 'ignore_mailto_links' ) && $link->is_mailto() ) { |
| 76 | $link->set_ignore(); |
| 77 | } |
| 78 | |
| 79 | // ignore WP Admin Bar Links |
| 80 | if ( $link->has_attr_value( 'class', 'ab-item' ) ) { |
| 81 | $link->set_ignore(); |
| 82 | } |
| 83 | |
| 84 | // ignore links containing ignored classes |
| 85 | if ( $this->has_ignore_class( $link ) ) { |
| 86 | $link->set_ignore(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private function has_ignore_class( WPEL_Link $link ) |
| 91 | { |
| 92 | $ignore_classes = $this->opt( 'ignore_classes', 'exceptions' ); |
| 93 | $ignore_classes_arr = explode( ',', $ignore_classes ); |
| 94 | |
| 95 | foreach ( $ignore_classes_arr as $ignore_class ) { |
| 96 | if ( $link->has_attr_value( 'class', trim( $ignore_class ) ) ) { |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Filter for "_wpel_before_filter" |
| 106 | * @param string $content |
| 107 | * @return string |
| 108 | */ |
| 109 | protected function filter__wpel_before_filter_10000000000( $content ) |
| 110 | { |
| 111 | $ignore_tags = array( 'head' ); |
| 112 | |
| 113 | if ( $this->opt( 'ignore_script_tags' ) ) { |
| 114 | $ignore_tags[] = 'script'; |
| 115 | } |
| 116 | |
| 117 | foreach ( $ignore_tags as $tag_name ) { |
| 118 | $content = preg_replace_callback( |
| 119 | $this->get_tag_regexp( $tag_name ) |
| 120 | , $this->get_callback( 'skip_tag' ) |
| 121 | , $content |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | return $content; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Filter for "_wpel_after_filter" |
| 130 | * @param string $content |
| 131 | * @return string |
| 132 | */ |
| 133 | protected function filter__wpel_after_filter_10000000000( $content ) |
| 134 | { |
| 135 | return $this->restore_content_placeholders( $content ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param type $tag_name |
| 140 | * @return type |
| 141 | */ |
| 142 | protected function get_tag_regexp( $tag_name ) |
| 143 | { |
| 144 | return '/<'. $tag_name .'[\s.*>|>](.*?)<\/'. $tag_name .'[\s+]*>/is'; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Pregmatch callback |
| 149 | * @param array $matches |
| 150 | * @return string |
| 151 | */ |
| 152 | protected function skip_tag( $matches ) |
| 153 | { |
| 154 | $skip_content = $matches[ 0 ]; |
| 155 | return $this->get_placeholder( $skip_content ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Return placeholder text for given content |
| 160 | * @param string $placeholding_content |
| 161 | * @return string |
| 162 | */ |
| 163 | protected function get_placeholder( $placeholding_content ) |
| 164 | { |
| 165 | $placeholder = '<!--- WPEL PLACEHOLDER '. count( $this->content_placeholders ) .' --->'; |
| 166 | $this->content_placeholders[ $placeholder ] = $placeholding_content; |
| 167 | return $placeholder; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Restore placeholders with original content |
| 172 | * @param string $content |
| 173 | * @return string |
| 174 | */ |
| 175 | protected function restore_content_placeholders( $content ) |
| 176 | { |
| 177 | foreach ( $this->content_placeholders as $placeholder => $placeholding_content ) { |
| 178 | $content = str_replace( $placeholder, $placeholding_content, $content ); |
| 179 | } |
| 180 | |
| 181 | return $content; |
| 182 | } |
| 183 | |
| 184 | } |
| 185 |