initial
This commit is contained in:
+79
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* The class for related author.
|
||||
*
|
||||
* @package Generateblocks/Extend/QueryLoop
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* The class for related author.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
class GenerateBlocks_Pro_Related_Author extends GenerateBlocks_Pro_Singleton {
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Author::include_current_author' );
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Author::exclude_current_author' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include posts of current post author to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
public static function include_current_author( $query_args ) {
|
||||
return self::add_current_author( $query_args, 'author__in' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude posts of current post author to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
public static function exclude_current_author( $query_args ) {
|
||||
return self::add_current_author( $query_args, 'author__not_in' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include current author to a query argument.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
* @param string $key The query argument key.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
protected static function add_current_author( $query_args, $key ) {
|
||||
if (
|
||||
isset( $query_args[ $key ] ) &&
|
||||
in_array( 'current-post-author', $query_args[ $key ] )
|
||||
) {
|
||||
$current_post_author_index = array_search( 'current-post-author', $query_args[ $key ] );
|
||||
array_splice( $query_args[ $key ], $current_post_author_index, 1 );
|
||||
|
||||
if ( ! in_array( get_the_author_meta( 'ID' ), $query_args[ $key ] ) ) {
|
||||
$query_args[ $key ][] = get_the_author_meta( 'ID' );
|
||||
}
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateBlocks_Pro_Related_Author::get_instance();
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* The class for related parent.
|
||||
*
|
||||
* @package Generateblocks/Extend/QueryLoop
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* The class for related parent.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
class GenerateBlocks_Pro_Related_Parent extends GenerateBlocks_Pro_Singleton {
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Parent::include_current_post' );
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Parent::exclude_current_post' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include current post as parent to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
public static function include_current_post( $query_args ) {
|
||||
return self::add_current_post( $query_args, 'post_parent__in' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude current post as parent to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
public static function exclude_current_post( $query_args ) {
|
||||
return self::add_current_post( $query_args, 'post_parent__not_in' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include current post to a query argument.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
* @param string $key The query argument key.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
protected static function add_current_post( $query_args, $key ) {
|
||||
if (
|
||||
isset( $query_args[ $key ] ) &&
|
||||
in_array( 'current-post', $query_args[ $key ] ) &&
|
||||
get_post_type() === $query_args['post_type']
|
||||
) {
|
||||
$current_post_index = array_search( 'current-post', $query_args[ $key ] );
|
||||
array_splice( $query_args[ $key ], $current_post_index, 1 );
|
||||
|
||||
if ( ! in_array( get_the_ID(), $query_args[ $key ] ) ) {
|
||||
$query_args[ $key ][] = get_the_ID();
|
||||
}
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateBlocks_Pro_Related_Parent::get_instance();
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* The class for related post.
|
||||
*
|
||||
* @package Generateblocks/Extend/QueryLoop
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* The related posts class.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
class GenerateBlocks_Pro_Related_Post extends GenerateBlocks_Pro_Singleton {
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Post::exclude_current_post' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude current post from the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param Array $query_args The query arguments.
|
||||
*
|
||||
* @return Array The query arguments without current post.
|
||||
*/
|
||||
public static function exclude_current_post( $query_args ) {
|
||||
if (
|
||||
isset( $query_args['post__not_in'] ) &&
|
||||
in_array( 'exclude-current', $query_args['post__not_in'] ) &&
|
||||
get_post_type() === $query_args['post_type']
|
||||
) {
|
||||
if ( ! in_array( get_the_ID(), $query_args['post__not_in'] ) ) {
|
||||
$query_args['post__not_in'][] = get_the_ID();
|
||||
}
|
||||
|
||||
$exclude_current_index = array_search( 'exclude-current', $query_args['post__not_in'] );
|
||||
array_splice( $query_args['post__not_in'], $exclude_current_index, 1 );
|
||||
|
||||
// This is to avoid current post being dynamically added to post__in which will show him in the result set.
|
||||
if (
|
||||
isset( $query_args['post__in'] ) &&
|
||||
in_array( get_the_ID(), $query_args['post__in'] )
|
||||
) {
|
||||
$current_post_index = array_search( get_the_ID(), $query_args['post__in'] );
|
||||
array_splice( $query_args['post__in'], $current_post_index, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateBlocks_Pro_Related_Post::get_instance();
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* The class for related terms.
|
||||
*
|
||||
* @package Generateblocks/Extend/QueryLoop
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* The class for related terms.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
class GenerateBlocks_Pro_Related_Terms extends GenerateBlocks_Pro_Singleton {
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Terms::include_current_post_terms' );
|
||||
add_filter( 'generateblocks_query_loop_args', 'GenerateBlocks_Pro_Related_Terms::exclude_current_post_terms' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include current post terms to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param Array $query_args The query arguments.
|
||||
*
|
||||
* @return Array The query arguments with current post terms.
|
||||
*/
|
||||
public static function include_current_post_terms( $query_args ) {
|
||||
return self::add_current_post_terms( $query_args, 'tax_query' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude current post terms to the query loop.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
*
|
||||
* @return array The query arguments without current post terms.
|
||||
*/
|
||||
public static function exclude_current_post_terms( $query_args ) {
|
||||
return self::add_current_post_terms( $query_args, 'tax_query_exclude' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include current post term to a query argument.
|
||||
*
|
||||
* @since 1.3.0
|
||||
* @param array $query_args The query arguments.
|
||||
* @param string $key The query argument key.
|
||||
*
|
||||
* @return array The query arguments.
|
||||
*/
|
||||
protected static function add_current_post_terms( $query_args, $key ) {
|
||||
if (
|
||||
is_singular() &&
|
||||
isset( $query_args[ $key ] )
|
||||
) {
|
||||
$query_args[ $key ] = array_map(
|
||||
function( $tax ) {
|
||||
if ( isset( $tax['terms'] ) && in_array( 'current-terms', $tax['terms'] ) ) {
|
||||
$registered_taxonomies = get_object_taxonomies( get_post_type() );
|
||||
|
||||
if ( in_array( $tax['taxonomy'], $registered_taxonomies ) ) {
|
||||
$related_terms = wp_get_object_terms(
|
||||
get_the_ID(),
|
||||
$tax['taxonomy'],
|
||||
array( 'fields' => 'ids' )
|
||||
);
|
||||
|
||||
$tax['terms'] = array_merge( $tax['terms'], $related_terms );
|
||||
}
|
||||
|
||||
$current_terms_index = array_search( 'current-terms', $tax['terms'] );
|
||||
array_splice( $tax['terms'], $current_terms_index, 1 );
|
||||
}
|
||||
|
||||
return $tax;
|
||||
},
|
||||
$query_args[ $key ]
|
||||
);
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateBlocks_Pro_Related_Terms::get_instance();
|
||||
Reference in New Issue
Block a user