diff --git a/.travis.yml b/.travis.yml index 4a61831..83dd41e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +sudo: false + notifications: email: on_success: never diff --git a/Gruntfile.js b/Gruntfile.js index 2413025..1029498 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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 diff --git a/includes/cache.php b/includes/cache.php index f8c3b9a..3bbf83e 100644 --- a/includes/cache.php +++ b/includes/cache.php @@ -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 ); @@ -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(); } @@ -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']; @@ -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' ); diff --git a/includes/defaults.php b/includes/defaults.php index f6669df..3ce9e66 100644 --- a/includes/defaults.php +++ b/includes/defaults.php @@ -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. * @@ -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. @@ -100,9 +100,9 @@ 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. * @@ -110,7 +110,7 @@ public static function init() { */ public function _setup() { - // Default taxonomies + // Default taxonomies. $this->all_tax = 'all'; // All taxonomies. $this->default_tax = array( 'category' => __( 'Category', 'related-posts-by-taxonomy' ) ); @@ -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. @@ -181,7 +209,6 @@ public function get_post_types() { return $post_types; } - /** * Returns all public taxonomies * Sets the id for 'All Taxonomies' @@ -222,7 +249,6 @@ public function get_taxonomies() { return array_unique( $tax ); } - /** * Returns all image sizes. * @@ -258,7 +284,6 @@ public function get_image_sizes() { return $sizes; } - /** * Returns all formats. * @@ -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( @@ -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' => '
', 'after_shortcode' => '
', 'before_title' => '

', 'after_title' => '

', - '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'] = '
'; + $settings['after_shortcode'] = '
'; + } + + $settings['type'] = $_type; return $settings; } } // end class - Related_Posts_By_Taxonomy_Defaults::init(); - } // class exists diff --git a/includes/functions.php b/includes/functions.php index 96abdd0..80ff0be 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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 ); @@ -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(); diff --git a/includes/shortcode.php b/includes/shortcode.php index 2b08b9c..c8c4d60 100644 --- a/includes/shortcode.php +++ b/includes/shortcode.php @@ -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 ''; diff --git a/includes/wp-rest-api.php b/includes/wp-rest-api.php new file mode 100644 index 0000000..d632d9a --- /dev/null +++ b/includes/wp-rest-api.php @@ -0,0 +1,291 @@ +[\d]+)', array( + 'args' => array( + 'id' => array( + 'description' => __( 'Unique identifier for the object.', 'related-posts-by-taxonomy' ), + 'type' => 'integer', + ), + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'args' => array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + } + + /** + * Get one item from the collection + * + * @since 2.3.0 + * @access public + * + * @param WP_REST_Request $request Full data about the request. + * @return WP_Error|WP_REST_Response + */ + public function get_item( $request ) { + $args = $request->get_params(); + $error = new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.', 'related-posts-by-taxonomy' ), array( 'status' => 404 ) ); + + if ( ! isset( $args['id'] ) || ( (int) $args['id'] <= 0 ) ) { + return $error; + } + + $post = get_post( (int) $args['id'] ); + if ( empty( $post ) || empty( $post->ID ) ) { + return $error; + } + + $defaults = km_rpbt_get_default_settings( 'wp_rest_api' ); + + /** + * Filter default wp_rest_api arguments. + * + * @since 2.3.0 + * + * @param array $defaults Default wp_rest_api arguments. + */ + $defaults = apply_filters( 'related_posts_by_taxonomy_wp_rest_api_defaults', $defaults ); + + $args['post_id'] = $args['id']; + $args = array_merge( $defaults, (array) $args ); + + /* Validates args. Sets the post types and post id if not set in filter above */ + $validated_args = km_rpbt_validate_shortcode_atts( (array) $args ); + $validated_args['type'] = 'wp-rest-api'; + + /** + * Filter wp_rest_api arguments. + * + * @since 2.3.0 + * + * @param array $args wp_rest_api arguments. + */ + $args = apply_filters( 'related_posts_by_taxonomy_wp_rest_api_args', $validated_args ); + $args = array_merge( $validated_args, (array) $args ); + $args['type'] = 'wp-rest-api'; + + $data = $this->prepare_item_for_response( $args, $request ); + return rest_ensure_response( $data ); + } + + + /** + * Check if a given request has access to get items + * + * @since 2.3.0 + * @access public + * + * @param WP_REST_Request $request Full data about the request. + * @return WP_Error|bool + */ + public function get_items_permissions_check( $request ) { + return apply_filters( 'related_posts_by_taxonomy_wp_rest_api', false ); + } + + /** + * Check if a given request has access to get a specific item + * + * @since 2.3.0 + * @access public + * + * @param WP_REST_Request $request Full data about the request. + * @return WP_Error|bool + */ + public function get_item_permissions_check( $request ) { + return $this->get_items_permissions_check( $request ); + } + + /** + * Prepare the item for the REST response + * + * @since 2.3.0 + * @access public + * + * @param array $args WP Rest API arguments of the item. + * @param WP_REST_Request $request Request object. + * @return mixed + */ + public function prepare_item_for_response( $args, $request ) { + + $post_id = $args['post_id']; + $taxonomies = $args['taxonomies']; + $related_posts = $this->get_related_posts( $post_id, $taxonomies, $args ); + + $data = array( + 'posts' => $related_posts, + 'termcount' => isset( $this->filter_args['termcount'] ) ? $this->filter_args['termcount'] : array(), + 'post_id' => $post_id, + 'post_types' => $args['post_types'], + 'taxonomies' => km_rpbt_get_taxonomies( $taxonomies ), + 'related_terms' => isset( $this->filter_args['related_terms'] ) ? $this->filter_args['related_terms'] : array(), + 'rendered' => km_rpbt_shortcode_output( $related_posts, $args ), + ); + + // Reset filter_args. + $this->filter_args = array(); + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + return $response; + } + + /** + * Retrieves the related post's schema, conforming to JSON Schema. + * + * @since 2.3.0 + * @access public + * + * @return array Item schema data. + */ + public function get_item_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/schema#', + 'title' => 'related_posts_by_tax', + 'type' => 'object', + 'properties' => array( + 'posts' => array( + 'description' => __( 'The related posts.', 'related-posts-by-taxonomy' ), + 'type' => 'array', + 'items' => array( + 'type' => 'object|string|integer', + ), + 'context' => array( 'view' ), + ), + 'termcount' => array( + 'description' => __( 'Number of related terms in common with the post.', 'related-posts-by-taxonomy' ), + 'type' => 'array', + 'items' => array( + 'type' => 'integer', + ), + 'context' => array( 'view' ), + ), + 'post_id' => array( + 'description' => __( 'The Post ID to get related posts for.', 'related-posts-by-taxonomy' ), + 'type' => 'integer', + 'context' => array( 'view' ), + ), + 'post_types' => array( + 'description' => __( 'Post types used in query for related posts.', 'related-posts-by-taxonomy' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'view' ), + ), + 'taxonomies' => array( + 'description' => __( 'Taxonomies used in query for related posts.', 'related-posts-by-taxonomy' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'view' ), + ), + 'related_terms' => array( + 'description' => __( 'Related term ids used in query for related posts.', 'related-posts-by-taxonomy' ), + 'type' => 'array', + 'items' => array( + 'type' => 'integer', + ), + 'context' => array( 'view' ), + ), + 'rendered' => array( + 'description' => __( 'Rendered related posts HTML', 'related-posts-by-taxonomy' ), + 'type' => 'string', + 'context' => array( 'view' ), + ), + ), + ); + + return $this->add_additional_fields_schema( $schema ); + } + + /** + * Returns arguments used for the related posts query. + * + * @since 2.3.0 + * @access public + * + * @param array $results Related posts. Array with Post objects or post IDs or post titles or post slugs. + * @param int $post_id Post id used to get the related posts. + * @param array $taxonomies Taxonomies used to get the related posts. + * @param array $args Function arguments used to get the related posts. + * @return array Related Posts. + */ + public function get_filter_args( $results, $post_id, $taxonomies, $args ) { + $this->filter_args = $args; + return $results; + } + + /** + * Returns related posts from database or cache. + * + * @since 2.3.0 + * @access public + * + * @param int $post_id Post id used to get the related posts. + * @param array $taxonomies Taxonomies used to get the related posts. + * @param array $args Function arguments used to get the related posts. + * @return array Related Posts. + */ + public function get_related_posts( $post_id, $taxonomies, $args ) { + $function_args = $args; + $plugin = km_rpbt_plugin(); + + unset( $function_args['post_id'], $function_args['taxonomies'], $args['id'] ); + + $cache = $plugin->cache instanceof Related_Posts_By_Taxonomy_Cache; + + add_filter( 'related_posts_by_taxonomy', array( $this, 'get_filter_args' ), 10, 4 ); + + if ( $cache && ( isset( $args['cache'] ) && $args['cache'] ) ) { + $related_posts = $plugin->cache->get_related_posts( $args ); + } else { + /* get related posts */ + $related_posts = km_rpbt_related_posts_by_taxonomy( $post_id, $taxonomies, $function_args ); + } + + remove_filter( 'related_posts_by_taxonomy', array( $this, 'get_filter_args' ), 10, 4 ); + + return $related_posts; + } +} diff --git a/lang/related-posts-by-taxonomy.pot b/lang/related-posts-by-taxonomy.pot index 73bd794..7867b99 100644 --- a/lang/related-posts-by-taxonomy.pot +++ b/lang/related-posts-by-taxonomy.pot @@ -2,28 +2,28 @@ # This file is distributed under the GPL v2. msgid "" msgstr "" -"Project-Id-Version: Related Posts By Taxonomy 2.2.3-alpha\n" +"Project-Id-Version: Related Posts By Taxonomy 2.3.0-beta1\n" "Report-Msgid-Bugs-To: " "https://wordpress.org/support/plugin/related-posts-by-taxonomy\n" -"POT-Creation-Date: 2017-03-26 11:58:30+00:00\n" +"POT-Creation-Date: 2017-05-18 11:12:01+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "PO-Revision-Date: 2017-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" -"X-Generator: grunt-wp-i18n 0.5.4\n" -"X-Poedit-KeywordsList: " -"__;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;_nx_noop:1,2,3c;esc_" -"attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;esc_html_x:1,2c;\n" "Language: en\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" "X-Poedit-Country: United States\n" "X-Poedit-SourceCharset: UTF-8\n" +"X-Poedit-KeywordsList: " +"__;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;_nx_noop:1,2,3c;esc_" +"attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;esc_html_x:1,2c;\n" "X-Poedit-Basepath: ../\n" "X-Poedit-SearchPath-0: .\n" "X-Poedit-Bookmarks: \n" "X-Textdomain-Support: yes\n" +"X-Generator: grunt-wp-i18n1.0.0\n" #: includes/defaults.php:115 msgid "Category" @@ -37,23 +37,23 @@ msgstr "" msgid "Thumbnail" msgstr "" -#: includes/defaults.php:271 +#: includes/defaults.php:296 msgid "Links" msgstr "" -#: includes/defaults.php:272 +#: includes/defaults.php:297 msgid "Posts" msgstr "" -#: includes/defaults.php:273 +#: includes/defaults.php:298 msgid "Excerpts" msgstr "" -#: includes/defaults.php:274 +#: includes/defaults.php:299 msgid "Post thumbnails" msgstr "" -#: includes/defaults.php:296 +#: includes/defaults.php:325 msgid "Related Posts" msgstr "" @@ -137,6 +137,42 @@ msgstr "" msgid "Display related posts for post ID (optional)" msgstr "" +#: includes/wp-rest-api.php:42 +msgid "Unique identifier for the object." +msgstr "" + +#: includes/wp-rest-api.php:70 +msgid "Invalid post ID." +msgstr "" + +#: includes/wp-rest-api.php:196 +msgid "The related posts." +msgstr "" + +#: includes/wp-rest-api.php:204 +msgid "Number of related terms in common with the post." +msgstr "" + +#: includes/wp-rest-api.php:212 +msgid "The Post ID to get related posts for." +msgstr "" + +#: includes/wp-rest-api.php:217 +msgid "Post types used in query for related posts." +msgstr "" + +#: includes/wp-rest-api.php:225 +msgid "Taxonomies used in query for related posts." +msgstr "" + +#: includes/wp-rest-api.php:233 +msgid "Related term ids used in query for related posts." +msgstr "" + +#: includes/wp-rest-api.php:241 +msgid "Rendered related posts HTML" +msgstr "" + #: templates/related-posts-excerpts.php:35 templates/related-posts-links.php:36 #: templates/related-posts-posts.php:35 #: templates/related-posts-thumbnails.php:56 diff --git a/package.json b/package.json index 26f69c6..3580952 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "related-posts-by-taxonomy", - "version": "2.2.2", + "version": "2.3.0-beta1", "tested_up_to": "4.7", "requires_at_least": "4.0", "main": "Gruntfile.js", @@ -10,13 +10,13 @@ "url": "https://github.com/keesiemeijer/related-posts-by-taxonomy.git" }, "devDependencies": { - "grunt": "^0.4.5", - "grunt-contrib-clean": "^0.6.0", - "grunt-contrib-copy": "^0.7.0", + "grunt": "^1.0.1", + "grunt-contrib-clean": "^1.1.0", + "grunt-contrib-copy": "^1.0.0", "grunt-gitinfo": "^0.1.7", "grunt-text-replace": "^0.4.0", "grunt-version": "^1.0.0", - "grunt-wp-i18n": "^0.5.2", + "grunt-wp-i18n": "^1.0.0", "load-grunt-tasks": "^3.2.0" } } diff --git a/phpunit.xml b/phpunit.xml index af587c9..a0f460e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,6 +10,7 @@ tests/test-misc.php tests/test-functions.php + tests/test-wp-rest-api.php tests/test-shortcode.php tests/test-widget.php tests/test-functions-thumbnail.php diff --git a/readme.md b/readme.md index 488bf40..7bbb188 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ -# [Related Posts by Taxonomy](http://keesiemeijer.wordpress.com/related-posts-by-taxonomy) [![Build Status](https://travis-ci.org/keesiemeijer/related-posts-by-taxonomy.svg?branch=master)](http://travis-ci.org/keesiemeijer/related-posts-by-taxonomy) # +# [Related Posts by Taxonomy](http://keesiemeijer.wordpress.com/related-posts-by-taxonomy) [![Build Status](https://travis-ci.org/keesiemeijer/related-posts-by-taxonomy.svg?branch=develop)](http://travis-ci.org/keesiemeijer/related-posts-by-taxonomy) # -Version: 2.2.2 +Version: 2.3.0-beta1 Requires at least: 4.0 Tested up to: 4.7 @@ -8,7 +8,7 @@ Tested up to: 4.7 This is the development repository for the WordPress plugin [Related Posts by Taxonomy](https://wordpress.org/plugins/related-posts-by-taxonomy). The `master` branch is where you'll find the most recent, stable release. -The `develop` branch is the current working branch for development. Both branches are required to pass all unit tests. Any pull requests are first merged with the `develop` branch before being merged into the `master` branch. See [Pull Requests](https://github.com/keesiemeijer/related-posts-by-taxonomy/tree/master#pull-requests) +The `develop` branch is the current working branch for development. Both branches are required to pass all unit tests. Any pull requests are first merged with the `develop` branch before being merged into the `master` branch. See [Pull Requests](https://github.com/keesiemeijer/related-posts-by-taxonomy/tree/develop#pull-requests) ## Description ## This WordPress plugin displays related posts as thumbnails, links, excerpts or as full posts with a widget or shortcode. Posts with the **most terms in common** will display at the top. Use multiple taxonomies and post types to get the related posts. Include or exclude terms. Change the look and feel with your own html templates in your (child) theme. diff --git a/readme.txt b/readme.txt index 1f9c11b..680d35c 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: keesiemeijer Tags: posts,related,related posts,related thumbnails,similar,similar posts,widget,shortcode,taxonomy,taxonomies,post type,post types,category,categories,tag,tags,post thumbnail,post thumbnails,thumbnails,featured,featured image,image,images Requires at least: 4.0 Tested up to: 4.7 -Stable tag: 2.2.2 +Stable tag: 2.3.0-beta1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -24,12 +24,13 @@ Plugin features: * Widget and Shortcode. * Display related posts as **post thumbnails**, links, excerpts or full posts. * **Small Footprint**. Doesn't slow down your site! -* Object **and** persistent cache to get the related posts. * Automatic display of related posts after the post content. * Search for related posts in single or multiple **taxonomies** and **post types**. * **Exclude** or **include** terms and posts. * **Limit the search** of related posts by date or number. * Use your own **HTML templates** for display of the related posts. +* Use the **WordPress REST API** to get related posts. (opt-in feature) +* Use a persistent cache layer for the related posts. (opt-in feature) * Use **plugin functions** in your theme templates to display related posts yourself. * Use Filters to **change the default behavior** of the plugin. * Extensive **[plugin documentation](http://keesiemeijer.wordpress.com/related-posts-by-taxonomy/)**. @@ -115,6 +116,12 @@ Yes. See [this filter](http://keesiemeijer.wordpress.com/related-posts-by-taxono 4. Twenty Thirteen screenshot. Post thumbnails (after post content) and the widget == Changelog == += 2.3.0 = + +* Enhancement + * Add a WordPress REST API layer to get related posts (opt-in feature) + * Add new filter to activate the WordPress REST API layer + = 2.2.2 = * Enhancement @@ -228,5 +235,5 @@ Updated unit tests to be more reliable. * some minor bug fixing for the shortcode. == Upgrade Notice == -= 2.2.2 = -New option to link thumbnail captions (post titles) to posts in the widget and shortcode. \ No newline at end of file += 2.3.0 = +This update adds support to get related post with the WordPress REST API. This is an opt-in feature that has to be activated before you can use it (see the plugin documentation). \ No newline at end of file diff --git a/related-posts-by-taxonomy.php b/related-posts-by-taxonomy.php index a314170..1f72390 100644 --- a/related-posts-by-taxonomy.php +++ b/related-posts-by-taxonomy.php @@ -1,7 +1,7 @@ . */ -// Exit if accessed directly +// Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -38,36 +38,39 @@ } /* loads plugin files, adds the shortcode and sets the text domain */ -if ( !function_exists( 'related_posts_by_taxonomy_init' ) ) { +if ( ! function_exists( 'related_posts_by_taxonomy_init' ) ) { function related_posts_by_taxonomy_init() { load_plugin_textdomain( 'related-posts-by-taxonomy', '', dirname( plugin_basename( __FILE__ ) ) . '/lang' ); - // defaults needed for this plugin + // Defaults used by this plugin. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/defaults.php'; - // deprecated functions + // Deprecated functions. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/deprecated.php'; - // the widget + // The widget. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/widget.php'; - // functions to retrieve related posts from the database + // Functions to retrieve related posts from the database. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/functions.php'; - // functions for display of the related post thumbnail gallery + // Functions for display of the related post thumbnail gallery. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/functions-thumbnail.php'; - // loads the different templates used for the widget and shortcode + // loads the different templates used for the widget and shortcode. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/template-loader.php'; - // displays the related posts from the shortcode + // displays the related posts from the shortcode. require_once RELATED_POSTS_BY_TAXONOMY_PLUGIN_DIR . 'includes/shortcode.php'; + + // Instantiate the defaults class. + Related_Posts_By_Taxonomy_Defaults::init(); } /* initialize plugin */ related_posts_by_taxonomy_init(); -} // !function_exists \ No newline at end of file +} // !function_exists diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fb689af..40ea9c4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,4 +9,4 @@ function _manually_load_plugin() { tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); require $_tests_dir . '/includes/bootstrap.php'; -require dirname( __FILE__ ) . '/../tests/utils.php'; \ No newline at end of file +require dirname( __FILE__ ) . '/../tests/testcase.php'; \ No newline at end of file diff --git a/tests/test-cache.php b/tests/test-cache.php index 6013c7d..6769126 100644 --- a/tests/test-cache.php +++ b/tests/test-cache.php @@ -2,30 +2,12 @@ /** * Tests for the widget in /includes/widget.php */ -class KM_RPBT_Cache_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms. - * - * @var object - */ - private $utils; +class KM_RPBT_Cache_Tests extends KM_RPBT_UnitTestCase { private $args = null; private $plugin; - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - function tearDown() { // use tearDown for WP < 4.0 remove_filter( 'related_posts_by_taxonomy_cache', '__return_true' ); @@ -62,11 +44,11 @@ function test_cache_setup() { * @depends KM_RPBT_Functions_Tests::test_km_rpbt_plugin */ function test_cache_filter() { - add_filter( 'related_posts_by_taxonomy_cache', array( $this->utils, 'return_bool' ) ); + add_filter( 'related_posts_by_taxonomy_cache', array( $this, 'return_bool' ) ); $plugin = km_rpbt_plugin(); $plugin->_setup(); - $this->assertFalse( $this->utils->boolean ); - $this->utils->boolean = null; + $this->assertFalse( $this->boolean ); + $this->boolean = null; } @@ -91,7 +73,7 @@ function test_cache_with_shortcode_in_post_content() { $this->setup_cache(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // Add a shortcode to cache. @@ -105,7 +87,7 @@ function test_cache_with_shortcode_in_post_content() { $this->go_to( get_permalink( $posts[0] ) ); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); // Trigger cache. ob_start(); @@ -113,7 +95,7 @@ function test_cache_with_shortcode_in_post_content() { the_content(); $content = ob_get_clean(); - $meta_key = $this->utils->get_cache_meta_key(); + $meta_key = $this->get_cache_meta_key(); // Cache should be set for the shortcode in $post[0] content. $this->assertNotEmpty( $meta_key ); @@ -144,14 +126,14 @@ function test_manually_cache_related_posts() { $this->setup_cache(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $args = array( 'fields' => 'ids' ); $taxonomies = array( 'post_tag' ); $related_posts = km_rpbt_cache_related_posts( $posts[1], $taxonomies, $args ); - $meta_key = $this->utils->get_cache_meta_key(); + $meta_key = $this->get_cache_meta_key(); // Cache should be set for $post[1]. $this->assertNotEmpty( $meta_key ); @@ -179,7 +161,7 @@ function test_custom_post_type_cache() { register_post_type( 'rel_cpt', array( 'taxonomies' => array( 'post_tag', 'rel_ctax' ) ) ); register_taxonomy( 'rel_ctax', 'rel_cpt' ); - $posts = $this->utils->create_posts_with_terms( 'rel_cpt', 'post_tag', 'rel_ctax' ); + $posts = $this->create_posts_with_terms( 'rel_cpt', 'post_tag', 'rel_ctax' ); $posts = $posts['posts']; $args = array( 'post_types' => array( 'rel_cpt' ) ); @@ -218,7 +200,7 @@ function test_flush_cache() { $this->setup_cache(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $args = array( 'fields' => 'ids' ); @@ -228,12 +210,12 @@ function test_flush_cache() { $related_posts = km_rpbt_cache_related_posts( $posts[2], $taxonomies, $args ); // Cache should be set for $post[2]. - $this->assertNotEmpty( $this->utils->get_cache_meta_key() ); + $this->assertNotEmpty( $this->get_cache_meta_key() ); km_rpbt_flush_cache(); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); } @@ -247,7 +229,7 @@ function test_cache_delete_post() { $this->setup_cache(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // Was set to true with create_posts_with_terms() @@ -260,7 +242,7 @@ function test_cache_delete_post() { $related_posts = km_rpbt_cache_related_posts( $posts[2], $taxonomies, $args ); // Cache should be set for $post[2]. - $this->assertNotEmpty( $this->utils->get_cache_meta_key() ); + $this->assertNotEmpty( $this->get_cache_meta_key() ); wp_delete_post( $posts[2] ); @@ -268,7 +250,7 @@ function test_cache_delete_post() { $this->plugin->cache->shutdown_flush_cache(); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); } @@ -281,9 +263,9 @@ function disabled_test_cache_set_post_thumbnail() { global $wpdb; $this->setup_cache(); - $attachment_id = $this->utils->create_image(); + $attachment_id = $this->create_image(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // Was set to true with create_posts_with_terms() @@ -296,7 +278,7 @@ function disabled_test_cache_set_post_thumbnail() { $related_posts = km_rpbt_cache_related_posts( $posts[2], $taxonomies, $args ); // Cache should be set for $post[2]. - $this->assertNotEmpty( $this->utils->get_cache_meta_key() ); + $this->assertNotEmpty( $this->get_cache_meta_key() ); set_post_thumbnail ( $posts[2], $attachment_id ); @@ -304,7 +286,7 @@ function disabled_test_cache_set_post_thumbnail() { $this->plugin->cache->shutdown_flush_cache(); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); } @@ -318,7 +300,7 @@ function test_cache_delete_term() { $this->setup_cache(); - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $terms = $create_posts['tax1_terms']; @@ -332,7 +314,7 @@ function test_cache_delete_term() { $related_posts = km_rpbt_cache_related_posts( $posts[2], $taxonomies, $args ); // Cache should be set for $post[2]. - $this->assertNotEmpty( $this->utils->get_cache_meta_key() ); + $this->assertNotEmpty( $this->get_cache_meta_key() ); wp_delete_term ( $terms[2], 'post_tag' ); @@ -340,7 +322,7 @@ function test_cache_delete_term() { $this->plugin->cache->shutdown_flush_cache(); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); } } \ No newline at end of file diff --git a/tests/test-debug.php b/tests/test-debug.php index 99154ae..aca1e22 100644 --- a/tests/test-debug.php +++ b/tests/test-debug.php @@ -3,26 +3,7 @@ /** * Tests for debug.php */ -class KM_RPBT_Debug_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms. - * - * @var object - */ - private $utils; - - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - +class KM_RPBT_Debug_Tests extends KM_RPBT_UnitTestCase { /** * Tests if debug filter is set to false (by default). @@ -30,11 +11,11 @@ function setUp() { * @depends KM_RPBT_Functions_Tests::test_km_rpbt_plugin */ function test_debug_filter() { - add_filter( 'related_posts_by_taxonomy_debug', array( $this->utils, 'return_bool' ) ); + add_filter( 'related_posts_by_taxonomy_debug', array( $this, 'return_bool' ) ); $plugin = km_rpbt_plugin(); $plugin->_setup(); - $this->assertFalse( $this->utils->boolean ); - $this->utils->boolean = null; + $this->assertFalse( $this->boolean ); + $this->boolean = null; } } \ No newline at end of file diff --git a/tests/test-deprecated.php b/tests/test-deprecated.php index 8c6dc75..22d0b09 100644 --- a/tests/test-deprecated.php +++ b/tests/test-deprecated.php @@ -3,7 +3,7 @@ /** * Tests for deprecated.php */ -class KM_RPBT_Deprecated_Tests extends WP_UnitTestCase { +class KM_RPBT_Deprecated_Tests extends KM_RPBT_UnitTestCase { /** * Tests for deprecated function km_rpbt_get_shortcode_atts(). diff --git a/tests/test-functions-thumbnail.php b/tests/test-functions-thumbnail.php index 5d8b5c0..122c5dc 100644 --- a/tests/test-functions-thumbnail.php +++ b/tests/test-functions-thumbnail.php @@ -3,26 +3,7 @@ /** * Tests for gallery in functions-thumbnail.php */ -class KM_RPBT_Gallery_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms - * - * @var object - */ - private $utils; - - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - +class KM_RPBT_Gallery_Tests extends KM_RPBT_UnitTestCase { function tearDown() { // use tearDown for WP < 4.0 @@ -258,7 +239,7 @@ function test_wordpress_gallery() { */ function setup_gallery() { - $posts = $this->utils->create_posts(); + $posts = $this->create_posts(); $related_post = get_post( $posts[0] ); $permalink = get_permalink( $related_post->ID ); diff --git a/tests/test-functions.php b/tests/test-functions.php index 6ab43fc..2ba173f 100644 --- a/tests/test-functions.php +++ b/tests/test-functions.php @@ -2,25 +2,18 @@ /** * Tests for the km_rpbt_related_posts_by_taxonomy() function in functions.php. */ -class KM_RPBT_Functions_Tests extends WP_UnitTestCase { +class KM_RPBT_Functions_Tests extends KM_RPBT_UnitTestCase { - private $utils; private $posts; private $tax_1_terms; private $tax_2_terms; private $taxonomies = array( 'category', 'post_tag' ); - function setUp() { - parent::setUp(); - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - - /** * Helper function to create 5 posts with 5 terms from two taxonomies. */ - function create_posts( $post_type = 'post', $tax1 = 'post_tag', $tax2 = 'category' ) { - $posts = $this->utils->create_posts_with_terms( $post_type, $tax1, $tax2 ); + function setup_posts( $post_type = 'post', $tax1 = 'post_tag', $tax2 = 'category' ) { + $posts = $this->create_posts_with_terms( $post_type, $tax1, $tax2 ); $this->posts = $posts['posts']; $this->tax_1_terms = $posts['tax1_terms']; $this->tax_2_terms = $posts['tax2_terms']; @@ -120,7 +113,7 @@ function test_km_rpbt_get_comma_separated_values() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_post_type_post() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; // Test with a single taxonomy. @@ -180,7 +173,7 @@ function test_custom_post_type_and_custom_taxonomy() { $this->assertFalse( is_taxonomy_hierarchical( 'rel_ctax' ) ); - $this->create_posts( 'rel_cpt', 'post_tag', 'rel_ctax' ); + $this->setup_posts( 'rel_cpt', 'post_tag', 'rel_ctax' ); $posts = $this->posts; $args = array( 'post_types' => array( 'rel_cpt', 'post' ), 'fields' => 'ids', ); @@ -234,7 +227,7 @@ function test_custom_post_type_and_custom_taxonomy() { */ function test_invalid_arguments() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; $args = array( 'fields' => 'ids' ); @@ -270,7 +263,7 @@ function test_invalid_arguments() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_exclude_terms() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'exclude_terms' => $this->tax_1_terms[2], 'fields' => 'ids' ); $rel_post0 = km_rpbt_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); $this->assertEquals( array( $this->posts[1], $this->posts[2] ), $rel_post0 ); @@ -283,7 +276,7 @@ function test_exclude_terms() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_include_terms() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'include_terms' => $this->tax_1_terms[0], 'fields' => 'ids' ); $rel_post0 = km_rpbt_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); $this->assertEquals( array( $this->posts[2] ), $rel_post0 ); @@ -296,7 +289,7 @@ function test_include_terms() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_include_terms_unrelated() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'include_terms' => array( $this->tax_2_terms[2], $this->tax_1_terms[3] ), 'related' => false, @@ -313,7 +306,7 @@ function test_include_terms_unrelated() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_related() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'related' => false, 'fields' => 'ids', @@ -329,7 +322,7 @@ function test_related() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_exclude_posts() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'exclude_posts' => $this->posts[2], 'fields' => 'ids' ); $rel_post0 = km_rpbt_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); $this->assertEquals( array( $this->posts[1], $this->posts[3] ), $rel_post0 ); @@ -342,7 +335,7 @@ function test_exclude_posts() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_limit_posts() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'limit_posts' => 2, 'fields' => 'ids' ); $rel_post0 = km_rpbt_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); $this->assertEquals( array( $this->posts[1], $this->posts[2] ), $rel_post0 ); @@ -355,7 +348,7 @@ function test_limit_posts() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_posts_per_page() { - $this->create_posts(); + $this->setup_posts(); $args = array( 'posts_per_page' => 1, 'fields' => 'ids' ); $rel_post3 = km_rpbt_related_posts_by_taxonomy( $this->posts[3], $this->taxonomies, $args ); $this->assertEquals( array( $this->posts[1] ), $rel_post3 ); @@ -368,7 +361,7 @@ function test_posts_per_page() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_fields() { - $this->create_posts(); + $this->setup_posts(); $_posts = get_posts( array( 'posts__in' => $this->posts, 'order' => 'post__in' ) ); $slugs = wp_list_pluck( $_posts, 'post_name' ); @@ -391,7 +384,7 @@ function test_fields() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_post_thumbnail() { - $this->create_posts(); + $this->setup_posts(); // Fake post thumbnails for post 1 and 3 add_post_meta( $this->posts[1], '_thumbnail_id' , 22 ); // fake attachment ID's @@ -409,7 +402,7 @@ function test_post_thumbnail() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_limit_month() { - $this->create_posts(); + $this->setup_posts(); $_posts = get_posts( array( 'posts__in' => $this->posts, 'order' => 'post__in' ) ); list( $date, $time ) = explode( ' ', $_posts[2]->post_date ); @@ -431,7 +424,7 @@ function test_limit_month() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_order_asc() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; $taxonomies = array( 'category', 'post_tag' ); @@ -448,7 +441,7 @@ function test_order_asc() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_order_asc_non_related() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; $taxonomies = array( 'category', 'post_tag' ); @@ -467,7 +460,7 @@ function test_order_asc_non_related() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_order_rand() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; $taxonomies = array( 'category', 'post_tag' ); @@ -489,7 +482,7 @@ function test_order_rand() { * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms */ function test_orderby_post_modified() { - $this->create_posts(); + $this->setup_posts(); $posts = $this->posts; $mypost = array( diff --git a/tests/test-misc.php b/tests/test-misc.php index 87aa0e2..b573979 100644 --- a/tests/test-misc.php +++ b/tests/test-misc.php @@ -2,34 +2,13 @@ /** * Tests for dependencies and various plugin functions */ -class KM_RPBT_Misc_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms to test with. - * - * @var object - */ - private $utils; - - private $boolean; - - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - +class KM_RPBT_Misc_Tests extends KM_RPBT_UnitTestCase { /** * Test if posts are created with the factory class. */ function test_create_posts() { - $posts = $this->utils->create_posts(); + $posts = $this->create_posts(); $this->assertNotEmpty( $posts ); return $posts; } @@ -42,7 +21,7 @@ function test_create_posts() { */ function test_assign_taxonomy_terms( array $posts ) { $this->assertNotEmpty( $posts ); - $terms = $this->utils->assign_taxonomy_terms( $posts, 'category', 1 ); + $terms = $this->assign_taxonomy_terms( $posts, 'category', 1 ); $this->assertNotEmpty( $terms ); } @@ -53,7 +32,7 @@ function test_assign_taxonomy_terms( array $posts ) { * Other test methods that create posts depend on this function to succeed. */ function test_create_posts_with_terms() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); //$create_posts = array(); $this->assertNotEmpty( $create_posts ); $this->assertCount( 5, $create_posts['posts'] ); @@ -88,7 +67,7 @@ function test_get_posts_by_author_sql() { * @expectedDeprecated km_rpbt_get_shortcode_atts */ function test_empty_output() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $args = array( 'fields' => 'ids' ); $taxonomies = array( 'category', 'post_tag' ); diff --git a/tests/test-shortcode.php b/tests/test-shortcode.php index 2983d54..90f2269 100644 --- a/tests/test-shortcode.php +++ b/tests/test-shortcode.php @@ -2,14 +2,7 @@ /** * Tests for the shortcode in shortcode.php */ -class KM_RPBT_Shortcode_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms - * - * @var object - */ - private $utils; +class KM_RPBT_Shortcode_Tests extends KM_RPBT_UnitTestCase { /** * Returned args from filter @@ -18,20 +11,9 @@ class KM_RPBT_Shortcode_Tests extends WP_UnitTestCase { */ private $args; - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - function tearDown() { // use tearDown for WP < 4.0 - remove_filter( 'related_posts_by_taxonomy_shortcode_hide_empty', array( $this->utils, 'return_bool' ) ); + remove_filter( 'related_posts_by_taxonomy_shortcode_hide_empty', array( $this, 'return_bool' ) ); remove_filter( 'related_posts_by_taxonomy_shortcode_hide_empty', '__return_true' ); remove_filter( 'related_posts_by_taxonomy_shortcode_atts', array( $this, 'return_args' ) ); @@ -90,11 +72,11 @@ function test_km_rpbt_validate_shortcode_atts() { */ function test_shortcode_hide_empty_filter_bool() { // shortcode - add_filter( 'related_posts_by_taxonomy_shortcode_hide_empty', array( $this->utils, 'return_bool' ) ); + add_filter( 'related_posts_by_taxonomy_shortcode_hide_empty', array( $this, 'return_bool' ) ); $id = $this->factory->post->create(); do_shortcode( '[related_posts_by_tax post_id="' . $id . '"]' ); - $this->assertTrue( $this->utils->boolean ); - $this->utils->boolean = null; + $this->assertTrue( $this->boolean ); + $this->boolean = null; } @@ -103,7 +85,7 @@ function test_shortcode_hide_empty_filter_bool() { */ function test_shortcode_hide_empty_filter() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; ob_start(); @@ -137,7 +119,7 @@ function test_shortcode_post_type() { ) ); // create posts for custom post type - $create_posts = $this->utils->create_posts_with_terms( 'cpt' ); + $create_posts = $this->create_posts_with_terms( 'cpt' ); $posts = $create_posts['posts']; // Add a shortcode to post content. @@ -170,7 +152,7 @@ function test_shortcode_post_type() { */ function test_related_posts_by_taxonomy_shortcode_atts() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // use filter to get arguments used for the related posts @@ -196,7 +178,7 @@ function test_related_posts_by_taxonomy_shortcode_atts() { */ function test_shortcode_output() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // get post ids array and permalinks array @@ -230,7 +212,7 @@ function test_shortcode_output() { */ function test_shortcode_related_value() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // use filter to get arguments used for the related posts @@ -266,7 +248,7 @@ function test_shortcode_related_value() { */ function test_shortcode_link_caption_value() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; // use filter to get arguments used for the related posts diff --git a/tests/test-uninstall.php b/tests/test-uninstall.php index 372875b..a03741a 100644 --- a/tests/test-uninstall.php +++ b/tests/test-uninstall.php @@ -3,24 +3,13 @@ /** * Tests for uninstall.php */ -class KM_RPBT_Uninstall_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms. - * - * @var object - */ - private $utils; - +class KM_RPBT_Uninstall_Tests extends KM_RPBT_UnitTestCase { /** * Set up. */ function setUp() { parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); delete_transient( 'rpbt_related_posts_flush_cache' ); } @@ -32,7 +21,7 @@ function setUp() { */ function test_uninstall() { global $wpdb; - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $args = array( 'fields' => 'ids' ); @@ -40,7 +29,7 @@ function test_uninstall() { $related_posts = km_rpbt_cache_related_posts( $posts[1], $taxonomies, $args ); // Test if cache for $post[1] exists. - $this->assertNotEmpty( $this->utils->get_cache_meta_key() ); + $this->assertNotEmpty( $this->get_cache_meta_key() ); $transient = set_transient( 'rpbt_related_posts_flush_cache', 1, DAY_IN_SECONDS * 5 ); @@ -56,6 +45,6 @@ function test_uninstall() { $this->assertFalse( $transient ); // Cache should be empty. - $this->assertEmpty( $this->utils->get_cache_meta_key() ); + $this->assertEmpty( $this->get_cache_meta_key() ); } } \ No newline at end of file diff --git a/tests/test-widget.php b/tests/test-widget.php index e3e2a5d..7bab759 100644 --- a/tests/test-widget.php +++ b/tests/test-widget.php @@ -2,14 +2,7 @@ /** * Tests for the widget in /includes/widget.php */ -class KM_RPBT_Widget_Tests extends WP_UnitTestCase { - - /** - * Utils object to create posts with terms. - * - * @var object - */ - private $utils; +class KM_RPBT_Widget_Tests extends KM_RPBT_UnitTestCase { /** * Widget settings. @@ -18,20 +11,9 @@ class KM_RPBT_Widget_Tests extends WP_UnitTestCase { */ private $settings; - - /** - * Set up. - */ - function setUp() { - parent::setUp(); - - // Use the utils class to create posts with terms. - $this->utils = new RPBT_Test_Utils( $this->factory ); - } - function tearDown() { // use tearDown for WP < 4.0 - remove_filter( 'related_posts_by_taxonomy_widget_hide_empty', array( $this->utils, 'return_bool' ) ); + remove_filter( 'related_posts_by_taxonomy_widget_hide_empty', array( $this, 'return_bool' ) ); remove_filter( 'related_posts_by_taxonomy_widget_hide_empty', '__return_false' ); parent::tearDown(); } @@ -52,10 +34,10 @@ function test_rpbt_widget_exists() { * Test if the widget_hide_empty filter is set to true (by default). */ function test_widget_hide_empty_filter_set_to_true() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; - add_filter( 'related_posts_by_taxonomy_widget_hide_empty', array( $this->utils, 'return_bool' ) ); + add_filter( 'related_posts_by_taxonomy_widget_hide_empty', array( $this, 'return_bool' ) ); $widget = new Related_Posts_By_Taxonomy( 'related-posts-by-taxonomy', __( 'Related Posts By Taxonomy', 'related-posts-by-taxonomy' ) ); // run the widget @@ -71,15 +53,15 @@ function test_widget_hide_empty_filter_set_to_true() { $widget->widget( $args, $instance ); $output = ob_get_clean(); - $this->assertTrue( $this->utils->boolean ); - $this->utils->boolean = null; + $this->assertTrue( $this->boolean ); + $this->boolean = null; } /** * Test if the widget_hide_empty filter if set to false. */ function test_widget_hide_empty_filter_set_to_false() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; add_filter( 'related_posts_by_taxonomy_widget_hide_empty', '__return_false' ); @@ -107,7 +89,7 @@ function test_widget_hide_empty_filter_set_to_false() { * Should be te similar to the arguments as for the related_posts_by_taxonomy_shortcode_atts filter */ function test_widget_filter_settings() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); add_filter( 'related_posts_by_taxonomy_widget_args', array( $this, 'return_settings' ) ); $widget = new Related_Posts_By_Taxonomy( 'related-posts-by-taxonomy', __( 'Related Posts By Taxonomy', 'related-posts-by-taxonomy' ) ); @@ -136,7 +118,7 @@ function test_widget_filter_settings() { * Test args validation. */ function test_widget_get_instance_settings() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $widget = new Related_Posts_By_Taxonomy( 'related-posts-by-taxonomy', __( 'Related Posts By Taxonomy', 'related-posts-by-taxonomy' ) ); $settings = $widget->get_instance_settings( array() ); @@ -154,7 +136,7 @@ function test_widget_get_instance_settings() { */ function test_rpbt_widget_output() { - $create_posts = $this->utils->create_posts_with_terms(); + $create_posts = $this->create_posts_with_terms(); $posts = $create_posts['posts']; $widget = new Related_Posts_By_Taxonomy( 'related-posts-by-taxonomy', __( 'Related Posts By Taxonomy', 'related-posts-by-taxonomy' ) ); diff --git a/tests/test-wp-rest-api.php b/tests/test-wp-rest-api.php new file mode 100644 index 0000000..06a1d52 --- /dev/null +++ b/tests/test-wp-rest-api.php @@ -0,0 +1,515 @@ +create_posts_with_terms( $post_type, $tax1, $tax2 ); + $this->posts = $posts['posts']; + $this->tax_1_terms = $posts['tax1_terms']; + $this->tax_2_terms = $posts['tax2_terms']; + } + + /** + * Returns related posts with the WordPress REST API. + * + * @param int $post_id The post id to get related posts for. + * @param array|string $taxonomies The taxonomies to retrieve related posts from. + * @param array|string $args Optional. Change what is returned. + * @return array|string Empty array if no related posts found. Array with post objects, or error code returned by the request. + */ + function rest_related_posts_by_taxonomy( $post_id = 0, $taxonomies = 'category', $args = '' ) { + + $request = new WP_REST_Request( 'GET', '/related-posts-by-taxonomy/v1/posts/' . $post_id ); + $request->set_param( 'taxonomies', $taxonomies ); + $args = is_array( $args ) ? $args : array( $args ); + foreach ( $args as $key => $value ) { + $request->set_param( $key, $value ); + } + + $response = rest_do_request( $request ); + $data = $response->get_data(); + + if ( isset( $data['code'] ) ) { + return $data['code']; + } + + return $data['posts']; + } + + /** + * Tests if wp_rest_api filter is set to false (by default). + * + * @depends KM_RPBT_Functions_Tests::test_km_rpbt_plugin + */ + function test_wp_rest_Api_filter() { + // Added by setUp(). + remove_filter( 'related_posts_by_taxonomy_wp_rest_api', '__return_true' ); + + $plugin = km_rpbt_plugin(); + $this->assertFalse( $plugin->plugin_supports( 'wp_rest_api' ) ); + } + + /** + * Test if the Related_Posts_By_Taxonomy_Rest_API class is loaded + * + * @requires function WP_REST_Controller::register_routes + */ + function test_wp_rest_api_class_is_loaded() { + $plugin = km_rpbt_plugin(); + global $wp_rest_server; + $wp_rest_server = new Spy_REST_Server; + do_action( 'rest_api_init' ); + $plugin->_setup_wp_rest_api(); + $this->assertTrue( class_exists( 'Related_Posts_By_Taxonomy_Rest_API' ) ); + $wp_rest_server = null; + } + + /** + * Test success response for rest request. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_wp_rest_api_success_response() { + $this->setup_posts(); + $posts = $this->posts; + + $request = new WP_REST_Request( 'GET', '/related-posts-by-taxonomy/v1/posts/' . $posts[0] ); + $request->set_param( 'fields', 'ids' ); + + $response = rest_do_request( $request ); + $data = $response->get_data(); + $expected = array( + 'posts', + 'termcount', + 'post_id', + 'post_types', + 'taxonomies', + 'related_terms', + 'rendered', + ); + + $this->assertEquals( $expected, array_keys( $data ) ); + } + + /** + * Test related posts for post type post. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_post_type_post() { + $this->setup_posts(); + $posts = $this->posts; + + // Test with a single taxonomy. + $taxonomies = array( 'post_tag' ); + $args = array( 'fields' => 'ids' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[2], $posts[1], $posts[3] ), $rel_post0 ); + + // Test post 1. + $rel_post1 = $this->rest_related_posts_by_taxonomy( $posts[1], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[2], $posts[3] ), $rel_post1 ); + + // Test post 2. + $rel_post2 = $this->rest_related_posts_by_taxonomy( $posts[2], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[1], $posts[3] ), $rel_post2 ); + + // Test post 3. + $rel_post3 = $this->rest_related_posts_by_taxonomy( $posts[3], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $rel_post3 ); + + // Test with multiple taxonomies. + $taxonomies = array( 'category', 'post_tag' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[1], $posts[2], $posts[3] ), $rel_post0 ); + + // Test post 1. + $rel_post1 = $this->rest_related_posts_by_taxonomy( $posts[1], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[3], $posts[2] ), $rel_post1 ); + + // Test post 2. + $rel_post2 = $this->rest_related_posts_by_taxonomy( $posts[2], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[1], $posts[3] ), $rel_post2 ); + + // Test post 3. + $rel_post3 = $this->rest_related_posts_by_taxonomy( $posts[3], $taxonomies, $args ); + $this->assertEquals( array( $posts[1], $posts[0], $posts[2] ), $rel_post3 ); + + // Test post 4. + $rel_post4 = $this->rest_related_posts_by_taxonomy( $posts[4], $taxonomies, $args ); + $this->assertEmpty( $rel_post4 ); + } + + /** + * Test related posts for custom post type and custom taxonomy. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_custom_post_type_and_custom_taxonomy() { + + register_post_type( 'rel_cpt', array( 'taxonomies' => array( 'post_tag', 'rel_ctax' ) ) ); + register_taxonomy( 'rel_ctax', 'rel_cpt' ); + + $this->assertFalse( is_taxonomy_hierarchical( 'rel_ctax' ) ); + + $this->setup_posts( 'rel_cpt', 'post_tag', 'rel_ctax' ); + $posts = $this->posts; + + $args = array( 'post_types' => array( 'rel_cpt', 'post' ), 'fields' => 'ids' ); + + // Test with a single taxonomy. + $taxonomies = array( 'rel_ctax' ); + + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[1] ), $rel_post0 ); + + // Test post 1. + $rel_post1 = $this->rest_related_posts_by_taxonomy( $posts[1], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[3] ), $rel_post1 ); + + // Test post 2. + $rel_post2 = $this->rest_related_posts_by_taxonomy( $posts[2], $taxonomies, $args ); + $this->assertEmpty( $rel_post2 ); + + // Test post 3. + $rel_post3 = $this->rest_related_posts_by_taxonomy( $posts[3], $taxonomies, $args ); + $this->assertEquals( array( $posts[1] ), $rel_post3 ); + + // Test with multiple taxonomies. + $taxonomies = array( 'rel_ctax', 'post_tag' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[1], $posts[2], $posts[3] ), $rel_post0 ); + + // Test post 1. + $rel_post1 = $this->rest_related_posts_by_taxonomy( $posts[1], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[3], $posts[2] ), $rel_post1 ); + + // Test post 2. + $rel_post2 = $this->rest_related_posts_by_taxonomy( $posts[2], $taxonomies, $args ); + $this->assertEquals( array( $posts[0], $posts[1], $posts[3] ), $rel_post2 ); + + // Test post 3. + $rel_post3 = $this->rest_related_posts_by_taxonomy( $posts[3], $taxonomies, $args ); + $this->assertEquals( array( $posts[1], $posts[0], $posts[2] ), $rel_post3 ); + + // Test post 4. + $rel_post4 = $this->rest_related_posts_by_taxonomy( $posts[4], $taxonomies, $args ); + $this->assertEmpty( $rel_post4 ); + } + + /** + * Test invalid function arguments. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_invalid_arguments() { + + $this->setup_posts(); + $posts = $this->posts; + + $args = array( 'fields' => 'ids' ); + + // Test single taxonomy. + $taxonomies = array( 'post_tag' ); + + // Not a post ID. + $fail = $this->rest_related_posts_by_taxonomy( 'not a post ID', $taxonomies, $args ); + $this->assertEquals( 'rest_no_route', $fail ); + + // Non existant post ID. + $fail2 = $this->rest_related_posts_by_taxonomy( 9999999999, $taxonomies, $args ); + $this->assertEquals( 'rest_post_invalid_id', $fail2 ); + + // Non existant taxonomy. + $fail3 = $this->rest_related_posts_by_taxonomy( $posts[0], 'not a taxonomy', $args ); + $this->assertEmpty( $fail3 ); + + // Empty string should default to taxonomy 'category'. + $fail4 = $this->rest_related_posts_by_taxonomy( $posts[0], '', $args ); + $this->assertEmpty( $fail4 ); + + // No arguments should return an empty array. + $fail5 = $this->rest_related_posts_by_taxonomy(); + $this->assertEquals( 'rest_post_invalid_id', $fail5 ); + } + + /** + * Test exclude_terms argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_exclude_terms() { + $this->setup_posts(); + $args = array( 'exclude_terms' => $this->tax_1_terms[2], 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[2] ), $rel_post0 ); + } + + /** + * Test include_terms argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_include_terms() { + $this->setup_posts(); + $args = array( 'include_terms' => $this->tax_1_terms[0], 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[2] ), $rel_post0 ); + } + + /** + * Test include_terms argument when related === false. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_include_terms_unrelated() { + $this->setup_posts(); + $args = array( + 'include_terms' => array( $this->tax_2_terms[2], $this->tax_1_terms[3] ), + 'related' => 'false', + 'fields' => 'ids', + ); + + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[3], $this->posts[4] ), $rel_post0 ); + } + + + /** + * Test related === false without include_terms. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_related() { + $this->setup_posts(); + $args = array( + 'related' => 'false', + 'fields' => 'ids', + ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[2], $this->posts[3] ), $rel_post0 ); + } + + /** + * Test exclude_posts function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_exclude_posts() { + $this->setup_posts(); + $args = array( 'exclude_posts' => $this->posts[2], 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[3] ), $rel_post0 ); + } + + /** + * Test limit_posts function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_limit_posts() { + $this->setup_posts(); + $args = array( 'limit_posts' => 2, 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[2] ), $rel_post0 ); + } + + /** + * Test posts_per_page function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_posts_per_page() { + $this->setup_posts(); + $args = array( 'posts_per_page' => 1, 'fields' => 'ids' ); + $rel_post3 = $this->rest_related_posts_by_taxonomy( $this->posts[3], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1] ), $rel_post3 ); + } + + /** + * Test fields function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_fields() { + $this->setup_posts(); + $_posts = get_posts( array( 'posts__in' => $this->posts, 'order' => 'post__in' ) ); + + $slugs = wp_list_pluck( $_posts, 'post_name' ); + $args = array( 'fields' => 'slugs' ); + + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $slugs[1], $slugs[2], $slugs[3] ), $rel_post0 ); + + $titles = wp_list_pluck( $_posts, 'post_title' ); + $args['fields'] = 'names'; + + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $titles[1], $titles[2], $titles[3] ), $rel_post0 ); + } + + /** + * Test post_thumbnail function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_post_thumbnail() { + $this->setup_posts(); + + // Fake post thumbnails for post 1 and 3 + add_post_meta( $this->posts[1], '_thumbnail_id' , 22 ); // Fake attachment ID's. + add_post_meta( $this->posts[3], '_thumbnail_id' , 33 ); + + $args = array( 'post_thumbnail' => true, 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[3] ), $rel_post0 ); + } + + /** + * Test limit_month function argument. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_limit_month() { + $this->setup_posts(); + $_posts = get_posts( array( 'posts__in' => $this->posts, 'order' => 'post__in' ) ); + + list( $date, $time ) = explode( ' ', $_posts[2]->post_date ); + $mypost = array( + 'ID' => $this->posts[2], + 'post_date' => date( 'Y-m-d H:i:s', strtotime( $date . ' -6 month' ) ), + ); + wp_update_post( $mypost ); + + $args = array( 'limit_month' => 2, 'fields' => 'ids' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $this->posts[0], $this->taxonomies, $args ); + $this->assertEquals( array( $this->posts[1], $this->posts[3] ), $rel_post0 ); + } + + /** + * Test ascending order. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_order_asc() { + $this->setup_posts(); + $posts = $this->posts; + + $taxonomies = array( 'category', 'post_tag' ); + $args = array( 'fields' => 'ids', 'order' => 'asc' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[2], $posts[1], $posts[3] ), $rel_post0 ); + } + + /** + * Test unrelated ascending order. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_order_asc_non_related() { + $this->setup_posts(); + $posts = $this->posts; + + $taxonomies = array( 'category', 'post_tag' ); + // 'false' is validated as false + $args = array( 'fields' => 'ids', 'order' => 'asc', 'related' => 'false' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + $this->assertEquals( array( $posts[3], $posts[2], $posts[1] ), $rel_post0 ); + } + + /** + * Test random order of posts. + * Todo: Find out how to test random results and apply + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_order_rand() { + $this->setup_posts(); + $posts = $this->posts; + + $taxonomies = array( 'category', 'post_tag' ); + $args = array( 'fields' => 'ids', 'order' => 'rand' ); + + // Test post 0. + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + + $this->assertContains( $posts[1], $rel_post0 ); + $this->assertContains( $posts[2], $rel_post0 ); + $this->assertContains( $posts[3], $rel_post0 ); + $this->assertEquals( count( $rel_post0 ), 3 ); + } + + /** + * Test order by post_modified. + * + * @depends KM_RPBT_Misc_Tests::test_create_posts_with_terms + * @requires function WP_REST_Controller::register_routes + */ + function test_orderby_post_modified() { + $this->setup_posts(); + $posts = $this->posts; + + $mypost = array( + 'ID' => $this->posts[2], + 'post_content' => 'new content', + ); + + // Update post_modified. + wp_update_post( $mypost ); + + $taxonomies = array( 'category', 'post_tag' ); + $args = array( 'fields' => 'ids', 'orderby' => 'post_modified' ); + $rel_post0 = $this->rest_related_posts_by_taxonomy( $posts[0], $taxonomies, $args ); + + // Test post 0. + $this->assertEquals( array( $posts[2], $posts[1], $posts[3] ), $rel_post0 ); + } + +} diff --git a/tests/utils.php b/tests/testcase.php similarity index 96% rename from tests/utils.php rename to tests/testcase.php index d2890e7..dc92733 100644 --- a/tests/utils.php +++ b/tests/testcase.php @@ -1,13 +1,15 @@ factory = $factory; + function setUp() { + parent::setUp(); } + function tearDown() { + parent::tearDown(); + } /** * Creates 5 posts and assigns terms from two taxonomies.