FilterHooks.php
275 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Filter Hooks class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers\Hooks; |
| 9 | |
| 10 | use Cassandra\Varint; |
| 11 | use RT\ThePostGrid\Helpers\Fns; |
| 12 | |
| 13 | // Do not allow directly accessing this file. |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 'This script cannot be accessed directly.' ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Filter Hooks class. |
| 20 | * |
| 21 | * @package RT_TPG |
| 22 | */ |
| 23 | class FilterHooks { |
| 24 | |
| 25 | /** |
| 26 | * Class init |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public static function init() { |
| 31 | add_filter( 'tpg_author_arg', [ __CLASS__, 'filter_author_args' ], 10 ); |
| 32 | add_filter( 'plugin_row_meta', [ __CLASS__, 'plugin_row_meta' ], 10, 2 ); |
| 33 | |
| 34 | $settings = get_option( 'rt_the_post_grid_settings' ); |
| 35 | |
| 36 | if ( isset( $settings['show_acf_details'] ) && $settings['show_acf_details'] ) { |
| 37 | add_filter( 'the_content', [ __CLASS__, 'tpg_acf_content_filter' ] ); |
| 38 | } |
| 39 | |
| 40 | add_filter( 'wp', [ __CLASS__, 'set_post_view_count' ], 9999 ); |
| 41 | add_filter( 'body_class', [ __CLASS__, 'body_classes' ] ); |
| 42 | add_filter( 'admin_body_class', [ __CLASS__, 'admin_body_class' ] ); |
| 43 | add_filter( 'wp_kses_allowed_html', [ __CLASS__, 'custom_wpkses_post_tags' ], 10, 2 ); |
| 44 | |
| 45 | //Query args modify |
| 46 | add_filter( 'tpg_sc_query_args', [ __CLASS__, 'modify_query_args' ], 10 ); |
| 47 | add_filter( 'tpg_sc_temp_query_args', [ __CLASS__, 'modify_query_args' ], 10 ); |
| 48 | |
| 49 | //add_filter( 'tpg_polylang', '__return_empty_string', 15 ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Modify main query args |
| 54 | * |
| 55 | * @param $args |
| 56 | * |
| 57 | * @return mixed |
| 58 | */ |
| 59 | public static function modify_query_args( $args ) { |
| 60 | if ( function_exists( 'pll_current_language' ) ) { |
| 61 | $language = pll_current_language(); |
| 62 | $args['lang'] = apply_filters( 'tpg_polylang', $language ); //If your site don't translate properly by poly language you need to return empty string. |
| 63 | } |
| 64 | |
| 65 | return $args; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add body classes |
| 70 | * |
| 71 | * @param $classes |
| 72 | * |
| 73 | * @return mixed |
| 74 | */ |
| 75 | public static function body_classes( $classes ) { |
| 76 | global $post; |
| 77 | |
| 78 | if ( isset( $post->post_content ) && has_shortcode( $post->post_content, 'tpg_my_account' ) ) { |
| 79 | $classes[] = 'tpg-myaccount-page'; |
| 80 | } |
| 81 | |
| 82 | $icon_font = Fns::tpg_option( 'tpg_icon_font' ); |
| 83 | $classes[] = 'rttpg'; |
| 84 | $classes[] = 'rttpg-' . RT_THE_POST_GRID_VERSION; |
| 85 | $classes[] = 'radius-frontend rttpg-body-wrap'; |
| 86 | if ( 'fontawesome' !== $icon_font ) { |
| 87 | $classes[] = 'rttpg-flaticon'; |
| 88 | } |
| 89 | |
| 90 | return $classes; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Admin body class |
| 95 | * |
| 96 | * @param string $classes Classes. |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public static function admin_body_class( $classes ) { |
| 101 | $settings = get_option( 'rt_the_post_grid_settings' ); |
| 102 | global $pagenow; |
| 103 | |
| 104 | if ( |
| 105 | isset( $settings['tpg_block_type'] ) && |
| 106 | in_array( $settings['tpg_block_type'], [ 'elementor', 'divi' ] ) |
| 107 | ) { |
| 108 | $classes .= ' tpg-block-type-el-or-divi'; |
| 109 | } |
| 110 | |
| 111 | // Check if the current page is post.php and if the post parameteris set. |
| 112 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 113 | if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) { |
| 114 | if ( rtTPG()->hasPro() ) { |
| 115 | $classes .= ' the-post-grid the-post-grid-pro'; |
| 116 | } else { |
| 117 | $classes .= ' the-post-grid'; |
| 118 | } |
| 119 | |
| 120 | $classes .= ' radius-editor rttpg-body-wrap'; |
| 121 | } |
| 122 | |
| 123 | return $classes; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Set view count |
| 129 | * |
| 130 | * @param string $content Content. |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | public static function set_post_view_count( $content ) { |
| 135 | if ( is_single() ) { |
| 136 | $pId = get_the_ID(); |
| 137 | Fns::update_post_views_count( $pId ); |
| 138 | } |
| 139 | |
| 140 | return $content; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Filter author args. |
| 145 | * |
| 146 | * @param array $args Args. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public static function filter_author_args( $args ) { |
| 151 | $defaults = [ 'role__in' => [ 'administrator', 'editor', 'author', 'contributor' ] ]; |
| 152 | |
| 153 | return wp_parse_args( $args, $defaults ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Add plugin row meta |
| 158 | * |
| 159 | * @param array $links Links. |
| 160 | * @param string $file File. |
| 161 | * |
| 162 | * @return array |
| 163 | */ |
| 164 | public static function plugin_row_meta( $links, $file ) { |
| 165 | if ( $file == RT_THE_POST_GRID_PLUGIN_ACTIVE_FILE_NAME ) { |
| 166 | $report_url = 'https://www.radiustheme.com/contact/'; |
| 167 | $row_meta['issues'] = sprintf( |
| 168 | '%2$s <a target="_blank" href="%1$s">%3$s</a>', |
| 169 | esc_url( $report_url ), |
| 170 | esc_html__( 'Facing issue?', 'the-post-grid' ), |
| 171 | '<span style="color: red">' . esc_html__( 'Please open a support ticket.', 'the-post-grid' ) . '</span>' |
| 172 | ); |
| 173 | |
| 174 | return array_merge( $links, $row_meta ); |
| 175 | } |
| 176 | |
| 177 | return (array) $links; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * ACF content filter |
| 182 | * |
| 183 | * @param string $content Content. |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | public static function tpg_acf_content_filter( $content ) { |
| 188 | // Check if we're inside the main loop in a post or page. |
| 189 | if ( is_single() && in_the_loop() && is_main_query() && rtTPG()->hasPro() ) { |
| 190 | $settings = get_option( rtTPG()->options['settings'] ); |
| 191 | |
| 192 | $data = [ |
| 193 | 'show_acf' => isset( $settings['show_acf_details'] ) && $settings['show_acf_details'] ? 'show' : false, |
| 194 | 'cf_group' => isset( $settings['cf_group_details'] ) ? $settings['cf_group_details'] : [], |
| 195 | 'cf_hide_empty_value' => isset( $settings['cf_hide_empty_value_details'] ) ? $settings['cf_hide_empty_value_details'] : false, |
| 196 | 'cf_show_only_value' => isset( $settings['cf_show_only_value_details'] ) ? $settings['cf_show_only_value_details'] : false, |
| 197 | 'cf_hide_group_title' => isset( $settings['cf_hide_group_title_details'] ) ? $settings['cf_hide_group_title_details'] : false, |
| 198 | ]; |
| 199 | |
| 200 | return $content . Fns::tpg_get_acf_data_elementor( $data, null, false ); |
| 201 | } |
| 202 | |
| 203 | return $content; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Add exceptions in wp_kses_post tags. |
| 209 | * |
| 210 | * @param array $tags Allowed tags, attributes, and/or entities. |
| 211 | * @param string $context Context to judge allowed tags by. Allowed values are 'post'. |
| 212 | * |
| 213 | * @return array |
| 214 | */ |
| 215 | |
| 216 | public static function custom_wpkses_post_tags( $tags, $context ) { |
| 217 | if ( 'post' === $context ) { |
| 218 | $tags['iframe'] = [ |
| 219 | 'src' => true, |
| 220 | 'height' => true, |
| 221 | 'width' => true, |
| 222 | 'frameborder' => true, |
| 223 | 'allowfullscreen' => true, |
| 224 | ]; |
| 225 | |
| 226 | $tags['input'] = [ |
| 227 | 'type' => true, |
| 228 | 'class' => true, |
| 229 | 'placeholder' => true, |
| 230 | 'name' => true, |
| 231 | ]; |
| 232 | |
| 233 | $tags['style'] = [ |
| 234 | 'src' => true, |
| 235 | ]; |
| 236 | |
| 237 | $tags['svg'] = [ |
| 238 | 'class' => true, |
| 239 | 'aria-hidden' => true, |
| 240 | 'aria-labelledby' => true, |
| 241 | 'role' => true, |
| 242 | 'xmlns' => true, |
| 243 | 'width' => true, |
| 244 | 'height' => true, |
| 245 | 'viewbox' => true, |
| 246 | 'stroke' => true, |
| 247 | 'fill' => true, |
| 248 | ]; |
| 249 | |
| 250 | $tags['g'] = [ |
| 251 | 'fill' => true, |
| 252 | ]; |
| 253 | |
| 254 | $tags['title'] = [ |
| 255 | 'title' => true, |
| 256 | ]; |
| 257 | |
| 258 | $tags['path'] = [ |
| 259 | 'd' => true, |
| 260 | 'fill' => true, |
| 261 | 'stroke-width' => true, |
| 262 | 'stroke-linecap' => true, |
| 263 | 'stroke-linejoin' => true, |
| 264 | 'fill-rule' => true, |
| 265 | 'clip-rule' => true, |
| 266 | 'stroke' => true, |
| 267 | 'transform' => true, |
| 268 | ]; |
| 269 | } |
| 270 | |
| 271 | return $tags; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 |