Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from jaredcobb/1.0.1
Browse files Browse the repository at this point in the history
Fixed terms not saving properly, added version validation.
  • Loading branch information
jaredcobb authored Jan 9, 2018
2 parents c891a3d + 975329c commit 012bb33
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 45 deletions.
6 changes: 5 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: jaredcobb
Tags: ccb, church, api, chms
Requires at least: 4.6.0
Tested up to: 4.9.1
Stable tag: 1.0.0
Stable tag: 1.0.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -61,6 +61,10 @@ You'll need to ensure your [group settings](https://churchcommunitybuilder.force

== Changelog ==

= 1.0.1 =
* Fixed a bug where terms are not populated in some circumstances
* Added checks for minimum PHP and WordPress versions to prevent crashes

= 1.0.0 =
* Official stable release
* *Breaking Changes* - Please note that post type and custom taxonomy names have changed (see [release notes](https://github.com/jaredcobb/ccb-core/wiki/1.0.0-Stable-Release) )
Expand Down
42 changes: 25 additions & 17 deletions ccb-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Church Community Builder Core API
* Plugin URI: https://www.wpccb.com
* Description: A plugin to provide a core integration of the Church Community Builder API into WordPress custom post types
* Version: 1.0.0
* Version: 1.0.1
* Author: Jared Cobb
* Author URI: https://www.jaredcobb.com/
* License: GPL-2.0+
Expand All @@ -27,24 +27,32 @@
define( 'CCB_CORE_PATH', plugin_dir_path( __FILE__ ) );
define( 'CCB_CORE_URL', plugin_dir_url( __FILE__ ) );
define( 'CCB_CORE_BASENAME', plugin_basename( __FILE__ ) );
define( 'CCB_CORE_VERSION', '1.0.0' );
define( 'CCB_CORE_VERSION', '1.0.1' );

// Code that runs during plugin activation and deactivation.
require_once CCB_CORE_PATH . 'includes/class-ccb-core-activator.php';
register_activation_hook( __FILE__, [ 'CCB_Core_Activator', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'CCB_Core_Activator', 'deactivate' ] );
// Check minimum requirements before proceeding.
require_once CCB_CORE_PATH . 'includes/class-ccb-core-requirements.php';
$ccb_core_requirements = new CCB_Core_Requirements();

// Internationalization, dashboard-specific hooks, and public-facing site hooks.
require_once CCB_CORE_PATH . 'includes/class-ccb-core.php';
if ( $ccb_core_requirements->requirements_met ) {

/**
* Begin execution of the plugin.
*
* @access public
* @return void
*/
function run_ccb_core() {
$plugin = new CCB_Core();
// Code that runs during plugin activation and deactivation.
require_once CCB_CORE_PATH . 'includes/class-ccb-core-activator.php';
register_activation_hook( __FILE__, [ 'CCB_Core_Activator', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'CCB_Core_Activator', 'deactivate' ] );

// Internationalization, dashboard-specific hooks, and public-facing site hooks.
require_once CCB_CORE_PATH . 'includes/class-ccb-core.php';

/**
* Begin execution of the plugin.
*
* @access public
* @return void
*/
function run_ccb_core() {
$plugin = new CCB_Core();

}
run_ccb_core();

}
run_ccb_core();
1 change: 0 additions & 1 deletion includes/class-ccb-core-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CCB_Core_Activator {
* @since 0.9.0
*/
public static function activate() {
// TODO: check dependencies like mcrypt and memory limits.
}

/**
Expand Down
112 changes: 112 additions & 0 deletions includes/class-ccb-core-requirements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* A class that validates plugin requirements before the plugin gets loaded.
*
* @link https://www.wpccb.com
* @package CCB_Core
* @subpackage CCB_Core/includes
*/

/**
* A class that validates plugin requirements before the plugin gets loaded.
*
* @package CCB_Core
* @subpackage CCB_Core/includes
* @author Jared Cobb <[email protected]>
*/
class CCB_Core_Requirements {

/**
* Whether or not the requirements are met
*
* @var bool
*/
public $requirements_met = true;

/**
* The minimum required version of PHP
*
* @var string
*/
private $required_php = '5.6.0';

/**
* The minimum required version of WordPress
*
* @var string
*/
private $required_wordpress = '4.6.0';

/**
* Any applicable error messages
*
* @var string
*/
private $error_message = '';

/**
* Initialize the class
*
* @return void
*/
public function __construct() {
$this->validate_versions();
}

/**
* Setup hooks to disable the plugin and show a notice
*
* @return void
*/
public function register_disable_plugin() {
add_action( 'admin_init', array( $this, 'disable_plugin' ) );
add_action( 'admin_notices', array( $this, 'send_error' ) );
}

/**
* Deactivate the plugin
*
* @return void
*/
public function disable_plugin() {
deactivate_plugins( CCB_CORE_BASENAME );
}

/**
* Display an admin notice if the server has failed minimum requirements.
*
* @return void
*/
public function send_error() {
echo '<div class="notice notice-error"><p>' . esc_html( $this->error_message ) . '</p></div>';
}

/**
* Ensure that the infrastructure supports the minimum
* requirements for PHP and WordPress versions
*
* @return void
*/
private function validate_versions() {
global $wp_version;

if ( version_compare( PHP_VERSION, $this->required_php, '<' ) ) {
$issue = 'PHP';
$version = $this->required_php;
} elseif ( version_compare( $wp_version, $this->required_wordpress, '<' ) ) {
$issue = 'WordPress';
$version = $this->required_wordpress;
} else {
return;
}

$this->requirements_met = false;
$this->error_message = sprintf(
'Church Community Builder Core API requires %1$s version %2$s or greater.',
$issue,
$version
);
$this->register_disable_plugin();
}

}
40 changes: 14 additions & 26 deletions includes/class-ccb-core-synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ public function insert_update_entities( $entities, $settings, $post_type ) {
'post_status' => 'publish',
'post_type' => $post_type,
'meta_input' => [],
'tax_input' => [],
];

// Inspect each field defined in the settings. If it's a
Expand All @@ -623,30 +622,6 @@ public function insert_update_entities( $entities, $settings, $post_type ) {
$args['meta_input']['ccb_modified_date'] = $this->auto_cast( $entity->modified );
}

// Prepare hierarchial taxonomies by ensuring the term id
// already exists and set terms ids.
$prepared_terms = $this->prepare_terms( $entity, $settings );
if ( ! empty( $prepared_terms ) ) {
$args['tax_input'] = $prepared_terms;
}

// If this is an update, we need to remove all term
// relationships in order to ensure we are in
// sync with the most recent entity. Otherwise if a
// relationship was removed by CCB, it'll be orphaned in WordPress.
if ( $args['ID'] ) {
if ( ! empty( $settings['taxonomies']['hierarchical'] ) ) {
foreach ( $settings['taxonomies']['hierarchical'] as $taxonomy => $node ) {
wp_delete_object_term_relationships( $args['ID'], $taxonomy );
}
}
if ( ! empty( $settings['taxonomies']['nonhierarchical'] ) ) {
foreach ( $settings['taxonomies']['nonhierarchical'] as $taxonomy => $node ) {
wp_delete_object_term_relationships( $args['ID'], $taxonomy );
}
}
}

/**
* Filters the `wp_insert_post` $args for each entity.
*
Expand Down Expand Up @@ -687,6 +662,15 @@ public function insert_update_entities( $entities, $settings, $post_type ) {
return $result;
}

// Prepare hierarchial taxonomies by ensuring the term id
// already exists and set terms ids.
$prepared_terms = $this->prepare_terms( $entity, $settings );
if ( ! empty( $prepared_terms ) ) {
foreach ( $prepared_terms as $taxonomy => $term_array ) {
$term_set_results = wp_set_object_terms( $post_id, $term_array, $taxonomy );
}
}

/**
* After the insert / update is processed.
*
Expand Down Expand Up @@ -793,8 +777,10 @@ public function prepare_terms( $entity, $settings ) {
$term = wp_insert_term( $term_name, $taxonomy );
}
if ( $term && ! is_wp_error( $term ) ) {
$categories[ $taxonomy ][] = $term['term_taxonomy_id'];
$categories[ $taxonomy ] = (int) $term['term_id'];
}
} else {
$categories[ $taxonomy ] = '';
}
}
}
Expand All @@ -805,6 +791,8 @@ public function prepare_terms( $entity, $settings ) {
$tag_is_set = $this->auto_cast( $entity->{$node} );
if ( $tag_is_set ) {
$tags[ $taxonomy ][] = $tag;
} else {
$tags[ $taxonomy ][] = '';
}
}
}
Expand Down

0 comments on commit 012bb33

Please sign in to comment.