database
5 months ago
importers
2 weeks ago
metabox
6 days ago
notifications
5 months ago
views
4 months ago
watcher
2 weeks ago
wizard
2 weeks ago
class-admin-bar-menu.php
2 weeks ago
class-admin-breadcrumbs.php
2 weeks ago
class-admin-dashboard-nav.php
2 weeks ago
class-admin-header.php
2 weeks ago
class-admin-helper.php
2 weeks ago
class-admin-init.php
8 months ago
class-admin-menu.php
2 weeks ago
class-admin.php
6 days ago
class-api.php
1 year ago
class-ask-review.php
2 weeks ago
class-assets.php
2 weeks ago
class-bulk-actions.php
2 weeks ago
class-cmb2-fields.php
2 weeks ago
class-cmb2-options.php
2 weeks ago
class-dashboard-widget.php
2 weeks ago
class-import-export.php
2 weeks ago
class-list-table.php
2 weeks ago
class-lock-modified-date.php
1 year ago
class-notices.php
2 weeks ago
class-option-center.php
2 weeks ago
class-options.php
2 weeks ago
class-page.php
2 weeks ago
class-post-columns.php
2 weeks ago
class-post-filters.php
2 weeks ago
class-pro-notice.php
2 weeks ago
class-register-options-page.php
9 months ago
class-registration.php
2 weeks ago
class-sanitize-settings.php
2 weeks ago
class-setup-wizard.php
2 weeks ago
index.php
7 years ago
class-dashboard-widget.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Dashboard Widget of the plugin. |
| 4 | * |
| 5 | * @since 1.0.81 |
| 6 | * @package RankMath |
| 7 | * @subpackage RankMath\Core |
| 8 | * @author Rank Math <support@rankmath.com> |
| 9 | */ |
| 10 | |
| 11 | namespace RankMath; |
| 12 | |
| 13 | use RankMath\KB; |
| 14 | use RankMath\Helper; |
| 15 | use RankMath\Traits\Hooker; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Dashboard_Widget class. |
| 21 | * |
| 22 | * @codeCoverageIgnore |
| 23 | */ |
| 24 | class Dashboard_Widget { |
| 25 | |
| 26 | use Hooker; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->action( 'wp_dashboard_setup', 'add_dashboard_widgets' ); |
| 33 | $this->action( 'rank_math/dashboard/widget', 'dashboard_widget_feed', 98 ); |
| 34 | $this->action( 'rank_math/dashboard/widget', 'dashboard_widget_footer', 99 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Register dashboard widget. |
| 39 | */ |
| 40 | public function add_dashboard_widgets() { |
| 41 | // Early Bail if action is not registered for the dashboard widget hook. |
| 42 | if ( |
| 43 | ( ! Helper::is_module_active( '404-monitor' ) || ! Helper::has_cap( '404_monitor' ) ) && |
| 44 | ( ! Helper::is_module_active( 'redirections' ) || ! Helper::has_cap( 'redirections' ) ) && |
| 45 | ( ! Helper::is_module_active( 'analytics' ) || ! Helper::has_cap( 'analytics' ) ) |
| 46 | ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $icon = '<span class="rank-math-icon"><svg viewBox="0 0 462.03 462.03" xmlns="http://www.w3.org/2000/svg" width="20"><g><path d="m462 234.84-76.17 3.43 13.43 21-127 81.18-126-52.93-146.26 60.97 10.14 24.34 136.1-56.71 128.57 54 138.69-88.61 13.43 21z"></path><path d="m54.1 312.78 92.18-38.41 4.49 1.89v-54.58h-96.67zm210.9-223.57v235.05l7.26 3 89.43-57.05v-181zm-105.44 190.79 96.67 40.62v-165.19h-96.67z"></path></g></svg></span>'; |
| 51 | |
| 52 | wp_add_dashboard_widget( |
| 53 | 'rank_math_dashboard_widget', |
| 54 | $icon . esc_html__( 'Rank Math Overview', 'seo-by-rank-math' ), |
| 55 | [ $this, 'render_dashboard_widget' ], |
| 56 | null, |
| 57 | null, |
| 58 | 'normal', |
| 59 | 'high' |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Render dashboard widget. |
| 65 | */ |
| 66 | public function render_dashboard_widget() { |
| 67 | echo '<div id="rank-math-dashboard-widget" class="rank-math-loading"></div>'; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Add Feed data in the admin dashboard widget. |
| 72 | */ |
| 73 | public function dashboard_widget_feed() { |
| 74 | $posts = $this->get_feed(); |
| 75 | ?> |
| 76 | <h3 class="rank-math-blog-title"><?php esc_html_e( 'Latest Blog Posts from Rank Math', 'seo-by-rank-math' ); ?></h3> |
| 77 | <?php if ( empty( $posts ) ) : ?> |
| 78 | <p><?php esc_html_e( 'Error: the Rank Math blog feed could not be downloaded.', 'seo-by-rank-math' ); ?></p> |
| 79 | <?php |
| 80 | return; |
| 81 | endif; |
| 82 | |
| 83 | echo '<ul class="rank-math-blog-list">'; |
| 84 | |
| 85 | $posts = $this->filter_posts( $posts ); |
| 86 | $label = $this->get_item_label( $posts ); |
| 87 | |
| 88 | foreach ( $posts as $index => $post ) : |
| 89 | $link = $this->add_utm_params( $post['link'], $index ); |
| 90 | ?> |
| 91 | <li class="rank-math-blog-post"> |
| 92 | <h4> |
| 93 | <?php if ( $label ) : ?> |
| 94 | <span class="rank-math-new-badge"><?php echo esc_html( $label ); ?></span> |
| 95 | <?php endif; ?> |
| 96 | <a target="_blank" href="<?php echo esc_url( $link ); ?>"> |
| 97 | <?php echo esc_html( $post['title']['rendered'] ); ?> |
| 98 | </a> |
| 99 | </h4> |
| 100 | </li> |
| 101 | <?php |
| 102 | $label = ''; |
| 103 | endforeach; |
| 104 | echo '</ul>'; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get label for first post. |
| 109 | * |
| 110 | * @param array $posts Posts. |
| 111 | */ |
| 112 | private function get_item_label( $posts ) { |
| 113 | $label = ''; |
| 114 | if ( ! empty( $posts[0]['custom_label'] ) ) { |
| 115 | $label = $posts[0]['custom_label']; |
| 116 | } |
| 117 | |
| 118 | $is_new = time() - strtotime( $posts[0]['date'] ) < 15 * DAY_IN_SECONDS; |
| 119 | if ( $is_new && empty( $label ) ) { |
| 120 | $label = esc_html__( 'NEW', 'seo-by-rank-math' ); |
| 121 | } |
| 122 | |
| 123 | return $label; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Filter posts by display condition. |
| 128 | * |
| 129 | * @param array $posts Posts. |
| 130 | */ |
| 131 | private function filter_posts( $posts ) { |
| 132 | $posts = array_filter( |
| 133 | $posts, |
| 134 | function ( $post ) { |
| 135 | if ( isset( $post['condition'] ) && 'is_free' === $post['condition'] && defined( 'RANK_MATH_PRO_FILE' ) ) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | ); |
| 142 | |
| 143 | return array_slice( $posts, 0, 3 ); // Max 3 posts. |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Add UTM tags to links. Only add if UTM params are not already present. |
| 148 | * |
| 149 | * @param string $link Link. |
| 150 | * @param int $index Array index. |
| 151 | */ |
| 152 | private function add_utm_params( $link, $index ) { |
| 153 | // Skip if link has any UTM tags already set. |
| 154 | if ( preg_match( '/[?&]utm_[a-z_]+=/', $link ) ) { |
| 155 | return $link; |
| 156 | } |
| 157 | |
| 158 | $utm_params = [ |
| 159 | 'utm_source' => 'Plugin', |
| 160 | 'utm_medium' => 'Dashboard%20Widget%20Post%20' . ( $index + 1 ), |
| 161 | 'utm_campaign' => 'WP', |
| 162 | ]; |
| 163 | |
| 164 | return add_query_arg( $utm_params, $link ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Add footer in the admin dashboard widget. |
| 169 | */ |
| 170 | public function dashboard_widget_footer() { |
| 171 | ?> |
| 172 | <div class="rank-math-widget-footer"> |
| 173 | <a target="_blank" href="<?php KB::the( 'blog', 'Dashboard Widget Blog' ); ?>"> |
| 174 | <?php esc_html_e( 'Blog', 'seo-by-rank-math' ); ?> |
| 175 | <span class="screen-reader-text"><?php esc_html_e( '(opens in a new window)', 'seo-by-rank-math' ); ?></span> |
| 176 | <span aria-hidden="true" class="dashicons dashicons-external"></span> |
| 177 | </a> |
| 178 | <a target="_blank" href="<?php KB::the( 'knowledgebase', 'Dashboard Widget Help' ); ?>"> |
| 179 | <?php esc_html_e( 'Help', 'seo-by-rank-math' ); ?> |
| 180 | <span class="screen-reader-text"><?php esc_html_e( '(opens in a new window)', 'seo-by-rank-math' ); ?></span> |
| 181 | <span aria-hidden="true" class="dashicons dashicons-external"></span> |
| 182 | </a> |
| 183 | <?php if ( ! defined( 'RANK_MATH_PRO_FILE' ) ) { ?> |
| 184 | <a target="_blank" href="<?php KB::the( 'pro', 'Dashboard Widget PRO' ); ?>" class="rank-math-widget-go-pro"> |
| 185 | <?php esc_html_e( 'Go Pro', 'seo-by-rank-math' ); ?> |
| 186 | <span class="screen-reader-text"><?php esc_html_e( '(opens in a new window)', 'seo-by-rank-math' ); ?></span> |
| 187 | <span aria-hidden="true" class="dashicons dashicons-external"></span> |
| 188 | </a> |
| 189 | <?php } ?> |
| 190 | </div> |
| 191 | <?php |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get posts. |
| 196 | */ |
| 197 | private function get_feed() { |
| 198 | $cache_key = 'rank_math_feed_posts_v2'; |
| 199 | $cache = get_transient( $cache_key ); |
| 200 | if ( false !== $cache ) { |
| 201 | return $cache; |
| 202 | } |
| 203 | |
| 204 | $response = wp_remote_get( 'https://rankmath.com/wp-json/wp/v2/posts?dashboard_widget_feed=1' ); |
| 205 | |
| 206 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 207 | set_transient( $cache_key, [], 2 * HOUR_IN_SECONDS ); |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | $posts = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 213 | |
| 214 | if ( empty( $posts ) || ! is_array( $posts ) ) { |
| 215 | set_transient( $cache_key, [], 2 * HOUR_IN_SECONDS ); |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | set_transient( $cache_key, $posts, 12 * HOUR_IN_SECONDS ); |
| 221 | |
| 222 | return $posts; |
| 223 | } |
| 224 | } |
| 225 |