FilterHooks.php
134 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 | * Filter Hooks class. |
| 19 | * |
| 20 | * @package RT_TPG |
| 21 | */ |
| 22 | class FilterHooks { |
| 23 | /** |
| 24 | * Class init |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public static function init() { |
| 29 | add_filter( 'tpg_author_arg', [ __CLASS__, 'filter_author_args' ], 10 ); |
| 30 | add_filter( 'plugin_row_meta', [ __CLASS__, 'plugin_row_meta' ], 10, 2 ); |
| 31 | |
| 32 | $settings = get_option( 'rt_the_post_grid_settings' ); |
| 33 | |
| 34 | if ( isset( $settings['show_acf_details'] ) && $settings['show_acf_details'] ) { |
| 35 | add_filter( 'the_content', [ __CLASS__, 'tpg_acf_content_filter' ] ); |
| 36 | } |
| 37 | |
| 38 | add_filter( 'wp_head', [ __CLASS__, 'set_post_view_count' ], 9999 ); |
| 39 | add_filter( 'admin_body_class', [ __CLASS__, 'admin_body_class' ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Admin body class |
| 44 | * |
| 45 | * @param string $clsses Classes. |
| 46 | * @return string |
| 47 | */ |
| 48 | public static function admin_body_class( $clsses ) { |
| 49 | $settings = get_option( 'rt_the_post_grid_settings' ); |
| 50 | |
| 51 | if ( isset( $settings['tpg_block_type'] ) && in_array( $settings['tpg_block_type'], [ 'elementor', 'shortcode' ], true ) ) { |
| 52 | $clsses .= ' tpg-block-type-elementor-or-shortcode'; |
| 53 | } |
| 54 | |
| 55 | return $clsses; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set view count |
| 60 | * |
| 61 | * @param string $content Content. |
| 62 | * @return string |
| 63 | */ |
| 64 | public static function set_post_view_count( $content ) { |
| 65 | if ( is_single() ) { |
| 66 | $pId = get_the_ID(); |
| 67 | Fns::update_post_views_count( $pId ); |
| 68 | } |
| 69 | |
| 70 | return $content; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Filter author args. |
| 75 | * |
| 76 | * @param array $args Args. |
| 77 | * @return array |
| 78 | */ |
| 79 | public static function filter_author_args( $args ) { |
| 80 | $defaults = [ 'role__in' => [ 'administrator', 'editor', 'author' ] ]; |
| 81 | |
| 82 | return wp_parse_args( $args, $defaults ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Add plugin row meta |
| 87 | * |
| 88 | * @param array $links Links. |
| 89 | * @param string $file File. |
| 90 | * @return array |
| 91 | */ |
| 92 | public static function plugin_row_meta( $links, $file ) { |
| 93 | if ( $file == RT_THE_POST_GRID_PLUGIN_ACTIVE_FILE_NAME ) { |
| 94 | $report_url = 'https://www.radiustheme.com/contact/'; |
| 95 | $row_meta['issues'] = sprintf( |
| 96 | '%2$s <a target="_blank" href="%1$s">%3$s</a>', |
| 97 | esc_url( $report_url ), |
| 98 | esc_html__( 'Facing issue?', 'the-post-grid' ), |
| 99 | '<span style="color: red">' . esc_html__( 'Please open a support ticket.', 'the-post-grid' ) . '</span>' |
| 100 | ); |
| 101 | |
| 102 | return array_merge( $links, $row_meta ); |
| 103 | } |
| 104 | |
| 105 | return (array) $links; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * ACF content filter |
| 110 | * |
| 111 | * @param string $content Content. |
| 112 | * @return string |
| 113 | */ |
| 114 | public static function tpg_acf_content_filter( $content ) { |
| 115 | // Check if we're inside the main loop in a post or page. |
| 116 | if ( is_single() && in_the_loop() && is_main_query() && rtTPG()->hasPro() ) { |
| 117 | $settings = get_option( rtTPG()->options['settings'] ); |
| 118 | |
| 119 | $data = [ |
| 120 | 'show_acf' => isset( $settings['show_acf_details'] ) && $settings['show_acf_details'] ? 'show' : false, |
| 121 | 'cf_group' => isset( $settings['cf_group_details'] ) ? $settings['cf_group_details'] : [], |
| 122 | 'cf_hide_empty_value' => isset( $settings['cf_hide_empty_value_details'] ) ? $settings['cf_hide_empty_value_details'] : false, |
| 123 | 'cf_show_only_value' => isset( $settings['cf_show_only_value_details'] ) ? $settings['cf_show_only_value_details'] : false, |
| 124 | 'cf_hide_group_title' => isset( $settings['cf_hide_group_title_details'] ) ? $settings['cf_hide_group_title_details'] : false, |
| 125 | ]; |
| 126 | |
| 127 | return $content . Fns::tpg_get_acf_data_elementor( $data, null, false ); |
| 128 | } |
| 129 | |
| 130 | return $content; |
| 131 | } |
| 132 | |
| 133 | } |
| 134 |