Skip to content

Commit

Permalink
Merge pull request #10 from Yoast/JM/optimization_1_1_1
Browse files Browse the repository at this point in the history
Refactored to make it more readable
  • Loading branch information
jcomack authored Jun 20, 2016
2 parents a6a874c + 1e68639 commit b588323
Showing 1 changed file with 101 additions and 60 deletions.
161 changes: 101 additions & 60 deletions yoast-acf-analysis.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Plugin Name: Yoast ACF Analysis
* Plugin Name: Yoast SEO: ACF Analysis
* Plugin URI: https://forsberg.ax
* Description: Adds the content of all ACF fields to the Yoast SEO score analysis.
* Version: 1.1.0
Expand All @@ -13,6 +13,10 @@
exit;
}

if ( ! defined( 'YOAST_ACF_ANALYSIS_FILE ' ) ) {
define( 'YOAST_ACF_ANALYSIS_FILE', __FILE__ );
}

/**
* Class Yoast_ACF_Analysis
*
Expand Down Expand Up @@ -45,95 +49,100 @@ public function admin_init() {

$notice_functions = array();

// ACF
if ( ! class_exists( 'acf' ) && ! is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
$notice_functions[] = 'acf_not_active_notification';
}

// Yoast SEO for WordPress
if ( ! defined( 'WPSEO_VERSION' ) ) {
$notice_functions[] = 'wordpress_seo_requirements_not_met';
// Check for: Yoast SEO for WordPress.
if ( defined( 'WPSEO_VERSION' ) ) {
// Make sure that version is >= 3.1
if ( version_compare( substr( WPSEO_VERSION, 0, 3 ), '3.1', '<' ) ) {
$notice_functions[] = 'wordpress_seo_requirements_not_met';
}
}
// Make sure that version is >= 3.1
else if ( version_compare( substr( WPSEO_VERSION, 0, 3 ), '3.1', '<' ) ) {
else {
$notice_functions[] = 'wordpress_seo_requirements_not_met';
}

// Stop here if we cannot do the job we are hired to do.
if ( ! empty( $notice_functions ) ) {
// Deactivate if installed as a plugin.
if ( current_user_can( 'activate_plugins' ) && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
foreach ( $notice_functions as $function ) {
add_action( 'admin_notices', array( $this, $function ) );
}
unset( $function );

$file = plugin_basename( __FILE__ );

deactivate_plugins( $file, false, is_network_admin() );
// Check for: ACF.
if ( ! class_exists( 'acf' ) && ! is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) {
$notice_functions[] = 'acf_not_active_notification';
}

// Add to recently active plugins list.
if ( ! is_network_admin() ) {
update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) );
} else {
update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) );
}
// Enqueue when no problems were found.
if ( empty( $notice_functions ) ) {
// Make sure we load very late to be able to check enqueue of scripts we depend on.
add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
}
else {
// Show notices to users who can act on them.
if ( current_user_can( 'activate_plugins' ) ) {
$this->show_notices( $notice_functions );

// Prevent trying again on page reload.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
// Deactivate this plugin if we are not a mu-plugin.
if ( is_plugin_active( plugin_basename( YOAST_ACF_ANALYSIS_FILE ) ) ) {
$this->deactivate();
}
}
}
else {
// Only enqueue when all requirements are met.
add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
}

/**
* Notify that we need ACF to be installed and active.
*/
public function acf_not_active_notification() {
$message = __( 'ACF Yoast Analysis requires Advanced Custom Fields (free or pro) to be installed and activated.', 'yoast-acf-analysis' );

printf( '<div class="error"><p>%s</p></div>', esc_html( $message ) );
$message = sprintf(
__( 'Please %1$sinstall & activate Advanced Custom Fields%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ),
'<a href="' . esc_url( admin_url( 'plugin-install.php?tab=search&type=term&s=advanced+custom+fields&plugin-search-input=Search+Plugins' ) ) . '">',
'</a>'
);

printf( '<div class="error"><p>%s</p></div>', $message );
}

/**
* Notify that we need Yoast SEO for WordPress to be installed and active.
*/
public function wordpress_seo_requirements_not_met() {
$message = __( 'ACF Yoast Analysis requires Yoast SEO for WordPress 3.1+ to be installed and activated.', 'yoast-acf-analysis' );
$message = sprintf(
__( 'Please %1$sinstall & activate Yoast SEO 3.1+%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ),
'<a href="' . esc_url( admin_url( 'plugin-install.php?tab=search&type=term&s=yoast+seo&plugin-search-input=Search+Plugins' ) ) . '">',
'</a>'
);

printf( '<div class="error"><p>%s</p></div>', esc_html( $message ) );
printf( '<div class="error"><p>%s</p></div>', $message );
}

/**
* Enqueue JavaScript file to feed data to Yoast Content Analyses.
*/
public function enqueue_scripts() {
// Post page enqueue.
wp_enqueue_script(
'yoast-acf-analysis-post',
plugins_url( '/js/yoast-acf-analysis.js', __FILE__ ),
array(
'jquery',
'wp-seo-post-scraper',
),
self::VERSION
);

// Term page enqueue.
wp_enqueue_script(
'yoast-acf-analysis-term',
plugins_url( '/js/yoast-acf-analysis.js', __FILE__ ),
array(
'jquery',
'wp-seo-term-scraper',
),
self::VERSION
);
// If the Asset Manager exists then we need to use a different prefix.
$script_prefix = ( class_exists( 'WPSEO_Admin_Asset_Manager' ) ? 'yoast-seo' : 'wp-seo' );

if ( wp_script_is( $script_prefix . '-post-scraper', 'enqueued' ) ) {
// Post page enqueue.
wp_enqueue_script(
$script_prefix . '-analysis-post',
plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ),
array(
'jquery',
$script_prefix . '-post-scraper',
),
self::VERSION
);
}

if ( wp_script_is( $script_prefix . '-term-scraper', 'enqueued' ) ) {
// Term page enqueue.
wp_enqueue_script(
$script_prefix . '-analysis-term',
plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ),
array(
'jquery',
$script_prefix . '-term-scraper',
),
self::VERSION
);
}
}

/**
Expand Down Expand Up @@ -220,6 +229,38 @@ private function get_field_data( $fields ) {

return trim( $output );
}

/**
* Show the notices that are queued
*
* @param array $notice_functions Array of functions to call.
*/
private function show_notices( $notice_functions ) {
foreach ( $notice_functions as $function ) {
add_action( 'admin_notices', array( $this, $function ) );
}
}

/**
* Deactivate this plugin
*/
private function deactivate() {
$file = plugin_basename( YOAST_ACF_ANALYSIS_FILE );
deactivate_plugins( $file, false, is_network_admin() );

// Add to recently active plugins list.
if ( ! is_network_admin() ) {
update_option( 'recently_activated', array( $file => $_SERVER['REQUEST_TIME'] ) + (array) get_option( 'recently_activated' ) );
}
else {
update_site_option( 'recently_activated', array( $file => $_SERVER['REQUEST_TIME'] ) + (array) get_site_option( 'recently_activated' ) );
}

// Prevent trying again on page reload.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}

new Yoast_ACF_Analysis();

0 comments on commit b588323

Please sign in to comment.