*/ namespace RankMath; use RankMath\KB; use RankMath\Helper; use RankMath\Traits\Hooker; defined( 'ABSPATH' ) || exit; /** * Dashboard_Widget class. * * @codeCoverageIgnore */ class Dashboard_Widget { use Hooker; /** * Constructor. */ public function __construct() { $this->action( 'wp_dashboard_setup', 'add_dashboard_widgets' ); $this->action( 'rank_math/dashboard/widget', 'dashboard_widget_feed', 98 ); $this->action( 'rank_math/dashboard/widget', 'dashboard_widget_footer', 99 ); } /** * Register dashboard widget. */ public function add_dashboard_widgets() { // Early Bail if action is not registered for the dashboard widget hook. if ( ( ! Helper::is_module_active( '404-monitor' ) || ! Helper::has_cap( '404_monitor' ) ) && ( ! Helper::is_module_active( 'redirections' ) || ! Helper::has_cap( 'redirections' ) ) && ( ! Helper::is_module_active( 'analytics' ) || ! Helper::has_cap( 'analytics' ) ) ) { return; } $icon = ''; wp_add_dashboard_widget( 'rank_math_dashboard_widget', $icon . esc_html__( 'Rank Math Overview', 'rank-math' ), [ $this, 'render_dashboard_widget' ], null, null, 'normal', 'high' ); } /** * Render dashboard widget. */ public function render_dashboard_widget() { echo '
'; } /** * Add Feed data in the admin dashboard widget. */ public function dashboard_widget_feed() { $posts = $this->get_feed(); ?>

'; $posts = $this->filter_posts( $posts ); $label = $this->get_item_label( $posts ); foreach ( $posts as $index => $post ) : $link = $this->add_utm_params( $post['link'], $index ); ?>
  • '; } /** * Get label for first post. * * @param array $posts Posts. */ private function get_item_label( $posts ) { $label = ''; if ( ! empty( $posts[0]['custom_label'] ) ) { $label = $posts[0]['custom_label']; } $is_new = time() - strtotime( $posts[0]['date'] ) < 15 * DAY_IN_SECONDS; if ( $is_new && empty( $label ) ) { $label = esc_html__( 'NEW', 'rank-math' ); } return $label; } /** * Filter posts by display condition. * * @param array $posts Posts. */ private function filter_posts( $posts ) { $posts = array_filter( $posts, function( $post ) { if ( isset( $post['condition'] ) && 'is_free' === $post['condition'] && defined( 'RANK_MATH_PRO_FILE' ) ) { return false; } return true; } ); return array_slice( $posts, 0, 3 ); // Max 3 posts. } /** * Add UTM tags to links. Only add if UTM params are not already present. * * @param string $link Link. * @param int $index Array index. */ private function add_utm_params( $link, $index ) { // Skip if link has any UTM tags already set. if ( preg_match( '/[?&]utm_[a-z_]+=/', $link ) ) { return $link; } $utm_params = [ 'utm_source' => 'Plugin', 'utm_medium' => 'Dashboard%20Widget%20Post%20' . ( $index + 1 ), 'utm_campaign' => 'WP', ]; return add_query_arg( $utm_params, $link ); } /** * Add footer in the admin dashboard widget. */ public function dashboard_widget_footer() { ?>