Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
* Adds `README.md` with basic information about plugin, installation,
usage and version support

* Adds PHP plugin class to initialize `js` file in correct section of
admin along with ensuring that both `Yoast SEO` and `CMB2` are running
as part of the WordPress instance

* Adds `js` class which utilises the `YoastSEO` js adapter to handle
the recalculation of SEO content scores on content pages
  • Loading branch information
harryfinn committed Apr 27, 2016
0 parents commit fc0368c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions js/yoast-cmb2-field-analysis.js
Original file line number Diff line number Diff line change
@@ -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);
91 changes: 91 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Plugin Name: Yoast CMB2 Field Analysis
* Plugin URI: https://harryfinn.co.uk
* Description: Adds the content of all CMB2 prefixed fields to the Yoast SEO score analysis.
* Version: 1.0.0
* Author: Harry Finn
* Author URI: https://harryfinn.co.uk
* License: GPL v3
*/

if(!defined('ABSPATH')) exit;

class YoastCMB2Analysis {
private $plugin_data = null;

public function __construct() {
add_action('admin_init', [$this, 'plugin_admin_setup']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
}

public function plugin_admin_setup() {
$this->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() {
?>

<div class="error">
<p>Yoast CMB2 Field Analysis requires CMB2 (plugin or library) to be installed and initialized.</p>
</div>

<?php
}

public function require_yoast_message() {
?>

<div class="error">
<p>Yoast CMB2 Field Analysis requires Yoast SEO 3.0+ to be installed and activated.</p>
</div>

<?php
}

public function enqueue_scripts($page_hook) {
if($page_hook !== 'post.php') return;

wp_register_script(
'yoast-cmb2-plugin-js',
plugins_url('js/yoast-cmb2-field-analysis.js', __FILE__),
['jquery', 'yoast-seo-post-scraper'],
$this->plugin_data['Version']
);
wp_enqueue_script('yoast-cmb2-plugin-js');
}
}

new YoastCMB2Analysis();

0 comments on commit fc0368c

Please sign in to comment.