| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Wincher dashboard widget. |
| 10 |
*/ |
| 11 |
class Wincher_Dashboard_Widget implements WPSEO_WordPress_Integration { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds an instance of the admin asset manager. |
| 15 |
* |
| 16 |
* @var WPSEO_Admin_Asset_Manager |
| 17 |
*/ |
| 18 |
protected $asset_manager; |
| 19 |
|
| 20 |
/** |
| 21 |
* Wincher_Dashboard_Widget constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$this->asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register WordPress hooks. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function register_hooks() { |
| 33 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_wincher_dashboard_assets' ] ); |
| 34 |
add_action( 'admin_init', [ $this, 'queue_wincher_dashboard_widget' ] ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Adds the Wincher dashboard widget if it should be shown. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function queue_wincher_dashboard_widget() { |
| 43 |
if ( $this->show_widget() ) { |
| 44 |
add_action( 'wp_dashboard_setup', [ $this, 'add_wincher_dashboard_widget' ] ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Adds the Wincher dashboard widget to WordPress. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function add_wincher_dashboard_widget() { |
| 54 |
add_filter( 'postbox_classes_dashboard_wpseo-wincher-dashboard-overview', [ $this, 'wpseo_wincher_dashboard_overview_class' ] ); |
| 55 |
wp_add_dashboard_widget( |
| 56 |
'wpseo-wincher-dashboard-overview', |
| 57 |
/* translators: %1$s expands to Yoast SEO, %2$s to Wincher */ |
| 58 |
sprintf( __( '%1$s / %2$s: Top Keyphrases', 'wordpress-seo' ), 'Yoast SEO', 'Wincher' ), |
| 59 |
[ $this, 'display_wincher_dashboard_widget' ], |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Adds CSS classes to the dashboard widget. |
| 65 |
* |
| 66 |
* @param array $classes An array of postbox CSS classes. |
| 67 |
* |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
public function wpseo_wincher_dashboard_overview_class( $classes ) { |
| 71 |
$classes[] = 'yoast wpseo-wincherdashboard-overview'; |
| 72 |
return $classes; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Displays the Wincher dashboard widget. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function display_wincher_dashboard_widget() { |
| 81 |
echo '<div id="yoast-seo-wincher-dashboard-widget"></div>'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Enqueues assets for the dashboard if the current page is the dashboard. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function enqueue_wincher_dashboard_assets() { |
| 90 |
if ( ! $this->is_dashboard_screen() ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$this->asset_manager->localize_script( 'wincher-dashboard-widget', 'wpseoWincherDashboardWidgetL10n', $this->localize_wincher_dashboard_script() ); |
| 95 |
$this->asset_manager->enqueue_script( 'wincher-dashboard-widget' ); |
| 96 |
$this->asset_manager->enqueue_style( 'wp-dashboard' ); |
| 97 |
$this->asset_manager->enqueue_style( 'monorepo' ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Translates strings used in the Wincher dashboard widget. |
| 102 |
* |
| 103 |
* @return array The translated strings. |
| 104 |
*/ |
| 105 |
public function localize_wincher_dashboard_script() { |
| 106 |
|
| 107 |
return [ |
| 108 |
'wincher_is_logged_in' => YoastSEO()->helpers->wincher->login_status(), |
| 109 |
'wincher_website_id' => WPSEO_Options::get( 'wincher_website_id', '' ), |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Checks if the current screen is the dashboard screen. |
| 115 |
* |
| 116 |
* @return bool Whether or not this is the dashboard screen. |
| 117 |
*/ |
| 118 |
private function is_dashboard_screen() { |
| 119 |
$current_screen = get_current_screen(); |
| 120 |
|
| 121 |
return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns true when the Wincher dashboard widget should be shown. |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
private function show_widget() { |
| 130 |
$analysis_seo = new WPSEO_Metabox_Analysis_SEO(); |
| 131 |
$user_can_edit = $analysis_seo->is_enabled() && current_user_can( 'edit_posts' ); |
| 132 |
$is_wincher_active = YoastSEO()->helpers->wincher->is_active(); |
| 133 |
|
| 134 |
return $user_can_edit && $is_wincher_active; |
| 135 |
} |
| 136 |
} |
| 137 |
|