Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees Meijer committed May 30, 2017
2 parents 9542ac7 + 7c165ce commit dc24ad6
Show file tree
Hide file tree
Showing 25 changed files with 1,070 additions and 293 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: php

sudo: false

notifications:
email:
on_success: never
Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module.exports = function( grunt ) {
grunt.registerTask( 'travis', [ 'gitinfo', 'replace:replace_branch' ] );

// Creates build
grunt.registerTask( 'build', [ 'clean:main', 'makepot', 'version', 'travis', 'copy:main' ] );
grunt.registerTask( 'build', [ 'clean:main', 'version', 'makepot', 'travis', 'copy:main' ] );

// Removes ALL development files in the root directory
// !!! be careful with this
Expand Down
8 changes: 4 additions & 4 deletions includes/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function get_related_posts( $args ) {
if ( empty( $cache['ids'] ) ) {
// Cached, but the current post has no related posts.
$posts = array();
$this->cache_log[] = sprintf( 'Post ID %d - cache exists empty', $args['post_id'] );
$this->cache_log[] = sprintf( 'Post ID %d - cache exists (no related posts found)', $args['post_id'] );
} else {
// Cached related post ids are found!
$posts = $this->get_cache( $args, $cache );
Expand Down Expand Up @@ -263,7 +263,7 @@ private function get_cache( $args, $cache ) {
}

if ( empty( $cache['ids'] ) ) {
$this->cache_log[] = sprintf( 'Post ID %d - cache exists empty', $args['post_id'] );
$this->cache_log[] = sprintf( 'Post ID %d - cache exists (no related posts found)', $args['post_id'] );
return array();
}

Expand Down Expand Up @@ -308,7 +308,7 @@ private function get_cache( $args, $cache ) {
$this->cache_log[] = sprintf( 'Post ID %d - cache exists', $args['post_id'] );
} else {
$posts = array();
$this->cache_log[] = sprintf( 'Post ID %d - cache exists empty', $args['post_id'] );
$this->cache_log[] = sprintf( 'Post ID %d - cache exists (no related posts found)', $args['post_id'] );
}

$post_id = $cache_args['post_id'];
Expand Down Expand Up @@ -552,7 +552,7 @@ public function display_cache_log( $wp_admin_bar ) {
}

if ( empty( $this->cache_log ) ) {
$this->cache_log[] = 'Cache not used';
$this->cache_log[] = 'This page has no related posts';
}

array_unshift( $this->cache_log, 'Related Posts Cache' );
Expand Down
113 changes: 77 additions & 36 deletions includes/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ class Related_Posts_By_Taxonomy_Defaults {
/**
* Class instance.
*
* @access private
*
* @since 0.2.1
* @see get_instance()
* @var object
*/
private static $instance = null;


/**
* Acces this plugin's working instance.
*
Expand All @@ -91,7 +92,6 @@ public static function get_instance() {
return self::$instance;
}


/**
* Sets up class properties on action hook wp_loaded.
* wp_loaded is fired after custom post types and taxonomies are registered by themes and plugins.
Expand All @@ -100,17 +100,17 @@ public static function get_instance() {
*/
public static function init() {
add_action( 'wp_loaded', array( self::get_instance(), '_setup' ) );
add_action( 'rest_api_init', array( self::get_instance(), '_setup_wp_rest_api' ) );
}


/**
* Sets up class properties.
*
* @since 0.2.1
*/
public function _setup() {

// Default taxonomies
// Default taxonomies.
$this->all_tax = 'all'; // All taxonomies.
$this->default_tax = array( 'category' => __( 'Category', 'related-posts-by-taxonomy' ) );

Expand All @@ -131,36 +131,64 @@ public function _setup() {

$this->formats = $this->get_formats();

/**
* Adds a cache layer for this plugin.
*
* @since 2.1.0
* @param bool $cache Default false
*/
$cache = apply_filters( 'related_posts_by_taxonomy_cache', false );

if ( $cache ) {
if ( $this->plugin_supports( 'cache' ) ) {
// Only load the cache class when $cache is set to true.
require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/cache.php';
$this->cache = new Related_Posts_By_Taxonomy_Cache();
}

/**
* Adds debug information to the footer.
*
* @since 2.0.0
* @param bool $debug Default false
*/
$debug = apply_filters( 'related_posts_by_taxonomy_debug', false );

if ( $debug && ! is_admin() ) {
if ( $this->plugin_supports( 'debug' ) && ! is_admin() ) {
// Only load the debug file when $debug is set to true.
require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/debug.php';
$debug = new Related_Posts_By_Taxonomy_Debug();
}
}

/**
* Sets up the WordPress REST API
*
* @since 2.3.0
*
* @return array Array with post type objects.
*/
public function _setup_wp_rest_api() {

// Class exists for WordPress 4.7 and up.
if ( ! class_exists( 'WP_REST_Controller' ) ) {
return;
}

if ( $this->plugin_supports( 'wp_rest_api' ) ) {
require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/wp-rest-api.php';

$rest_api = new Related_Posts_By_Taxonomy_Rest_API();
$rest_api->register_routes();
}
}

/**
* Adds opt in support with a filter for cache, WP REST API and debug.
*
* @since 2.3.0
*
* @param string $type Type of support ('cache', 'wp_rest_api', 'debug').
* @return bool True if set to true with a filter. Default false.
*/
public function plugin_supports( $type = '' ) {
if ( ! in_array( $type , array( 'cache', 'wp_rest_api', 'debug' ) ) ) {
return false;
}

/**
* Filter whether to support cache, wp_rest_api or debug.
*
* The dynamic portion of the hook name, `$type`, refers to the
* type type of support ('cache', 'wp_rest_api', 'debug').
*
* @param bool $bool Add support if true. Default false
*/
return apply_filters( "related_posts_by_taxonomy_{$type}", false );
}

/**
* Returns all public post types.
Expand All @@ -181,7 +209,6 @@ public function get_post_types() {
return $post_types;
}


/**
* Returns all public taxonomies
* Sets the id for 'All Taxonomies'
Expand Down Expand Up @@ -222,7 +249,6 @@ public function get_taxonomies() {
return array_unique( $tax );
}


/**
* Returns all image sizes.
*
Expand Down Expand Up @@ -258,7 +284,6 @@ public function get_image_sizes() {
return $sizes;
}


/**
* Returns all formats.
*
Expand All @@ -276,18 +301,27 @@ public function get_formats() {
return $formats;
}


/**
* Returns default settings for the shortcode and widget.
*
* @since 2.2.2
* @param tring $type Type of settings. Choose from 'widget', 'shortcode' or 'all'.
* @param tring $type Type of settings. Choose from 'widget', 'shortcode', 'wp_rest_api' or 'all'.
* @return string ype of settings. Values can be 'shortcode' or 'widget'
*/
public function get_default_settings( $type = '' ) {

// Settings for the km_rpbt_related_posts_by_taxonomy() function.
$defaults = km_rpbt_get_default_args();
$types = array(
'shortcode',
'widget',
'wp_rest_api',
);

$_type = $type;

// wp_rest_api settings are the same as a shortcode.
$type = ( 'wp_rest_api' === $type ) ? 'shortcode' : $type;

// Common settings for the widget and shortcode.
$settings = array(
Expand All @@ -303,35 +337,42 @@ public function get_default_settings( $type = '' ) {

$settings = array_merge( $defaults, $settings );

// No default setting for post types.
$settings['post_types'] = '';

if ( ! in_array( $type, $types ) ) {
return $settings;
}

// Custom settings for the widget.
if ( ( 'widget' === $type ) || ( 'all' === $type ) ) {
if ( ( 'widget' === $type ) ) {
$settings['random'] = false;
$settings['singular_template'] = false;
$settings['type'] = 'widget';
}

// Custom settings for the shortcode.
if ( ( 'shortcode' === $type ) || ( 'all' === $type ) ) {
if ( ( 'shortcode' === $type ) ) {
$shortcode_args = array(
'before_shortcode' => '<div class="rpbt_shortcode">',
'after_shortcode' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
'type' => 'shortcode',
);

$settings = array_merge( $settings, $shortcode_args );
}

// No default setting for post types.
$settings['post_types'] = '';
$settings['type'] = in_array( $type, array( 'shortcode', 'widget' ) ) ? $type : '';
// Custom settings for the WP rest API.
if ( 'wp_rest_api' === $_type ) {
$settings['before_shortcode'] = '<div class="rpbt_wp_rest_api">';
$settings['after_shortcode'] = '</div>';
}

$settings['type'] = $_type;

return $settings;
}

} // end class

Related_Posts_By_Taxonomy_Defaults::init();

} // class exists
10 changes: 10 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ function km_rpbt_related_posts_by_taxonomy( $post_id = 0, $taxonomies = 'categor

/* order related posts */
uasort( $results, 'km_rpbt_related_posts_by_taxonomy_cmp' );

/* add termcount to args so we can use it later */
$termcount = wp_list_pluck( array_values( $results ), 'score' );
foreach ( $termcount as $key => $count ) {
$termcount[ $key ] = $count[0];
}
$args['termcount'] = $termcount;
}

$results = array_values( $results );
Expand All @@ -288,6 +295,9 @@ function km_rpbt_related_posts_by_taxonomy( $post_id = 0, $taxonomies = 'categor
$posts_per_page = absint( $args['posts_per_page'] );
$posts_per_page = ( $posts_per_page ) ? $posts_per_page : 5;
$results = array_slice( $results, 0, $posts_per_page );
if ( isset( $args['termcount'] ) && $args['termcount'] ) {
$args['termcount'] = array_slice( $args['termcount'], 0, $posts_per_page );
}
}
} else {
$results = array();
Expand Down
4 changes: 2 additions & 2 deletions includes/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ function km_rpbt_related_posts_by_taxonomy_shortcode( $rpbt_args ) {
function km_rpbt_shortcode_output( $related_posts, $rpbt_args ) {

/* make sure all defaults are present */
$rpbt_args = array_merge( km_rpbt_get_default_settings( 'shortcode' ), $rpbt_args );
$rpbt_args = array_merge( km_rpbt_get_default_settings( $rpbt_args['type'] ), $rpbt_args );

/* get the template depending on the format */
$template = km_rpbt_related_posts_by_taxonomy_template( $rpbt_args['format'], 'shortcode' );
$template = km_rpbt_related_posts_by_taxonomy_template( $rpbt_args['format'], $rpbt_args['type'] );

if ( ! $template ) {
return '';
Expand Down
Loading

0 comments on commit dc24ad6

Please sign in to comment.