Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashboard wincher connect #20107

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions admin/class-admin-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ protected function scripts_to_be_registered() {
'api-client' => [ 'wp-api' ],
'crawl-settings' => [ 'jquery' ],
'dashboard-widget' => [ self::PREFIX . 'api-client' ],
'wincher-dashboard-widget' => [ self::PREFIX . 'api-client' ],
'editor-modules' => [ 'jquery' ],
'elementor' => [
self::PREFIX . 'api-client',
Expand Down
3 changes: 2 additions & 1 deletion admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function __construct() {
}

$this->admin_features = [
'dashboard_widget' => new Yoast_Dashboard_Widget(),
'dashboard_widget' => new Yoast_Dashboard_Widget(),
'wincher_dashboard_widget' => new Wincher_Dashboard_Widget(),
];

if ( WPSEO_Metabox::is_post_overview( $pagenow ) || WPSEO_Metabox::is_post_edit( $pagenow ) ) {
Expand Down
128 changes: 128 additions & 0 deletions admin/class-wincher-dashboard-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin
*/

/**
* Wincher dashboard widget.
*/
class Wincher_Dashboard_Widget implements WPSEO_WordPress_Integration {

/**
* Holds an instance of the admin asset manager.
*
* @var WPSEO_Admin_Asset_Manager
*/
protected $asset_manager;

/**
* Wincher_Dashboard_Widget constructor.
*/
public function __construct() {
$this->asset_manager = new WPSEO_Admin_Asset_Manager();
}

/**
* Register WordPress hooks.
*/
public function register_hooks() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_wincher_dashboard_assets' ] );
add_action( 'admin_init', [ $this, 'queue_wincher_dashboard_widget' ] );
}

/**
* Adds the Wincher dashboard widget if it should be shown.
*
* @return void
*/
public function queue_wincher_dashboard_widget() {
if ( $this->show_widget() ) {
add_action( 'wp_dashboard_setup', [ $this, 'add_wincher_dashboard_widget' ] );
}
}

/**
* Adds the Wincher dashboard widget to WordPress.
*/
public function add_wincher_dashboard_widget() {
add_filter( 'postbox_classes_dashboard_wpseo-wincher-dashboard-overview', [ $this, 'wpseo_wincher_dashboard_overview_class' ] );
wp_add_dashboard_widget(
'wpseo-wincher-dashboard-overview',
/* translators: %1$s expands to Yoast SEO, %2$s to Wincher */
sprintf( __( '%1$s / %2$s: Top Keyphrases', 'wordpress-seo' ), 'Yoast SEO', 'Wincher' ),
[ $this, 'display_wincher_dashboard_widget' ]
);
}

/**
* Adds CSS classes to the dashboard widget.
*
* @param array $classes An array of postbox CSS classes.
*
* @return array
*/
public function wpseo_wincher_dashboard_overview_class( $classes ) {
$classes[] = 'yoast wpseo-wincherdashboard-overview';
return $classes;
}

/**
* Displays the Wincher dashboard widget.
*/
public function display_wincher_dashboard_widget() {
echo '<div id="yoast-seo-wincher-dashboard-widget"></div>';
}

/**
* Enqueues assets for the dashboard if the current page is the dashboard.
*/
public function enqueue_wincher_dashboard_assets() {
if ( ! $this->is_dashboard_screen() ) {
return;
}

$this->asset_manager->localize_script( 'wincher-dashboard-widget', 'wpseoWincherDashboardWidgetL10n', $this->localize_wincher_dashboard_script() );
$this->asset_manager->enqueue_script( 'wincher-dashboard-widget' );
$this->asset_manager->enqueue_style( 'wp-dashboard' );
$this->asset_manager->enqueue_style( 'monorepo' );
}

/**
* Translates strings used in the Wincher dashboard widget.
*
* @return array The translated strings.
*/
public function localize_wincher_dashboard_script() {

return [
'wincher_is_logged_in' => YoastSEO()->helpers->wincher->login_status(),
'wincher_website_id' => WPSEO_Options::get( 'wincher_website_id', '' ),
];
}

/**
* Checks if the current screen is the dashboard screen.
*
* @return bool Whether or not this is the dashboard screen.
*/
private function is_dashboard_screen() {
$current_screen = get_current_screen();

return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
}

/**
* Returns true when the Wincher dashboard widget should be shown.
*
* @return bool
*/
private function show_widget() {
$analysis_seo = new WPSEO_Metabox_Analysis_SEO();
$user_can_edit = $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
$is_wincher_active = YoastSEO()->helpers->wincher->is_active();

return $user_can_edit && $is_wincher_active;
}
}
5 changes: 0 additions & 5 deletions admin/class-yoast-dashboard-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ public function enqueue_dashboard_assets() {
* @return array The translated strings.
*/
public function localize_dashboard_script() {
$is_wincher_active = YoastSEO()->helpers->wincher->is_active();

return [
'feed_header' => sprintf(
/* translators: %1$s resolves to Yoast.com */
Expand All @@ -127,9 +125,6 @@ public function localize_dashboard_script() {
'feed_footer' => __( 'Read more like this on our SEO blog', 'wordpress-seo' ),
'wp_version' => substr( $GLOBALS['wp_version'], 0, 3 ) . '-' . ( is_plugin_active( 'classic-editor/classic-editor.php' ) ? '1' : '0' ),
'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
'is_wincher_active' => ( $is_wincher_active ) ? 1 : 0,
'wincher_is_logged_in' => ( $is_wincher_active ) ? YoastSEO()->helpers->wincher->login_status() : false,
'wincher_website_id' => WPSEO_Options::get( 'wincher_website_id', '' ),
];
}

Expand Down
1 change: 1 addition & 0 deletions config/webpack/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const getEntries = ( sourceDirectory = "./packages/js/src" ) => ( {
"classic-editor": `${ sourceDirectory }/classic-editor.js`,
"crawl-settings": `${ sourceDirectory }/crawl-settings.js`,
"dashboard-widget": `${ sourceDirectory }/dashboard-widget.js`,
"wincher-dashboard-widget": `${ sourceDirectory }/wincher-dashboard-widget.js`,
"dynamic-blocks": `${ sourceDirectory }/dynamic-blocks.js`,
"edit-page": `${ sourceDirectory }/edit-page.js`,
"editor-modules": `${ sourceDirectory }/editor-modules.js`,
Expand Down
4 changes: 2 additions & 2 deletions css/src/dashboard.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading