diff --git a/README.md b/README.md new file mode 100644 index 0000000..3fc9f46 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Yoast CMB2 Field Analysis WP Plugin + +## About this plugin + +This plugin adds in a `js` based method of recalculating Yoast SEO's content +scores when updating page content, specifically custom meta fields added via the +CMB2 library. + +A `js` method is the required due to the `php` filters that used to allow for +recalculating being removed from the plugin towards the end of 2015. + +This plugin will work with either the CMB2 WordPress plugin or the library files +if used directly within your WordPress theme. + +## Installing + +To install, simply download a `.zip` version of this repository and upload to +your WordPress instance via the Admin screens, +`Plugins -> Add New -> Upload Plugin`. + +## Usage + +In order for your CMB2 field content to be calculated in the SEO score, ensure +that the field name (`id` field for group attributes) contains `_cmb2_`. +It is a good idea to use this as part of a prefixing structure for your CMB2 +fields but it can also be used in a number of different ways: + +- `_cmb2_hero_banner_text` +- `_my_theme_cmb2_hero_banner_text` (recommended) +- `_my_theme_hero_banner_cmb2_` + +## WordPress and Yoast SEO dependencies + +Currently, this plugin has been tested and working with WordPress +`4.5` and Yoast SEO `3.2.3` + +This plugin will be updated and retested with new versions of both +WordPress and the Yoast SEO plugin, with the version numbers above +updated to reflect this. diff --git a/js/yoast-cmb2-field-analysis.js b/js/yoast-cmb2-field-analysis.js new file mode 100644 index 0000000..5bae183 --- /dev/null +++ b/js/yoast-cmb2-field-analysis.js @@ -0,0 +1,30 @@ +(function($) { + $(function() { + YoastCMB2FieldAnalysis = function() { + YoastSEO.app.registerPlugin('YoastCMB2FieldAnalysis', {status: 'ready'}); + YoastSEO.app.registerModification( + 'content', + this.addAcfFieldsToContent, + 'YoastCMB2FieldAnalysis' + ); + + $('#post-body').find( + 'input[type=text][name*=_cmb2_], textarea[name*=_cmb2_]' + ).on('keyup paste cut', function() { + YoastSEO.app.pluginReloaded('YoastCMB2FieldAnalysis'); + }); + }; + + YoastCMB2FieldAnalysis.prototype.addAcfFieldsToContent = function(data) { + var cmb2_content; + + $('#post-body').find( + 'input[type=text][name*=_cmb2_], textarea[name*=_cmb2_]' + ).each(function() { cmb2_content += ' ' + $(this).val(); }); + + return data + cmb2_content; + }; + + new YoastCMB2FieldAnalysis(); + }); +})(jQuery); diff --git a/setup.php b/setup.php new file mode 100644 index 0000000..fb1030c --- /dev/null +++ b/setup.php @@ -0,0 +1,91 @@ +plugin_data = get_plugin_data(dirname(__FILE__)); + + if(current_user_can('activate_plugins')) { + $cmb2_active = $this->check_for_cmb2(); + $yoast_seo_active = $this->check_for_yoast_seo(); + $deactivate = $cmb2_active || $yoast_seo_active; + + if($deactivate) { + deactivate_plugins(plugin_basename(__FILE__)); + + if(!empty($_GET['activate'])) unset($_GET['activate']); + } + } + } + + public function check_for_cmb2() { + if(!is_plugin_active('cmb2/init.php') && !has_action('cmb2_init')) { + add_action('admin_notices', [$this, 'require_cmb2_message']); + + return true; + } + + return false; + } + + public function check_for_yoast_seo() { + if(!is_plugin_active('wordpress-seo/wp-seo.php')) { + add_action('admin_notices', [$this, 'require_yoast_message']); + + return true; + } + + return false; + } + + public function require_cmb2_message() { + ?> + +
+

Yoast CMB2 Field Analysis requires CMB2 (plugin or library) to be installed and initialized.

+
+ + + +
+

Yoast CMB2 Field Analysis requires Yoast SEO 3.0+ to be installed and activated.

+
+ + plugin_data['Version'] + ); + wp_enqueue_script('yoast-cmb2-plugin-js'); + } +} + +new YoastCMB2Analysis();