Skip to content

Commit

Permalink
Clean up files
Browse files Browse the repository at this point in the history
  • Loading branch information
keesiemeijer committed Dec 4, 2019
1 parent 653b265 commit dddad4f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 47 deletions.
52 changes: 26 additions & 26 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,6 @@ function cptda_is_valid_post_type( $post_type ) {
return ! empty( $valid );
}

/**
* Get all public post types
*
* Note: the 'attachment' and 'page' post type are not included.
*
* @since 2.6.2
*
* @return array Array with public post types.
*/
function cptda_get_public_post_types() {
$args = array(
'public' => true,
'publicly_queryable' => true, // excludes pages
);

$post_types = get_post_types( $args, 'objects', 'and' );

foreach ( (array) $post_types as $key => $value ) {
$post_types[ $key ] = esc_attr( $value->labels->menu_name );
}

unset( $post_types['attachment'] );

return $post_types;
}

/**
* Is the query for a custom post type date archive?
*
Expand Down Expand Up @@ -184,6 +158,32 @@ function cptda_get_post_type_base( $post_type = '' ) {
return $rewrite->get_base_permastruct();
}

/**
* Get all public post types including post type 'post'.
*
* Note: the 'attachment' and 'page' post type are not included.
*
* @since 2.6.2
*
* @return array Array with public post types.
*/
function cptda_get_public_post_types() {
$args = array(
'public' => true,
'publicly_queryable' => true, // excludes pages
);

$post_types = get_post_types( $args, 'objects', 'and' );

foreach ( (array) $post_types as $key => $value ) {
$post_types[ $key ] = esc_attr( $value->labels->menu_name );
}

unset( $post_types['attachment'] );

return $post_types;
}

/**
* Get classes for the editor blocks.
*
Expand Down
1 change: 0 additions & 1 deletion includes/utils/archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ function cptda_get_archives_html( $args ) {
$class .= $type_class;
}


$paged = isset( $args['page'] ) ? absint( $args['page'] ) : 0;
$paged = ( 1 < $paged ) ? $paged : 0;
if ( $paged ) {
Expand Down
12 changes: 6 additions & 6 deletions includes/utils/recent-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,16 @@ function cptda_validate_recent_posts_settings( $args ) {
* @return array Array with post objects.
*/
function cptda_get_recent_posts( $args ) {
$recent_posts = array();

if ( ! isset( $args['post_type'] ) ) {
return $recent_posts;
return array();
}

$post_types = cptda_get_public_post_types();
if ( in_array( $args['post_type'], array_keys( $post_types ) ) ) {
$recent_posts = get_posts( $args );
return get_posts( $args );
}

return $recent_posts;
return array();
}

/**
Expand All @@ -126,10 +124,12 @@ function cptda_get_recent_posts_html( $recent_posts, $args ) {
$is_block = false;
$html = '';
$args = cptda_validate_recent_posts_settings( $args );
$message = $args['message'] ? apply_filters( 'the_content', $args['message'] ) : '';
$class = isset( $args['class'] ) ? $args['class'] : '';
$class = sanitize_html_class( $class );

/** This filter is documented in wp-includes/post_template.php */
$message = $args['message'] ? apply_filters( 'the_content', $args['message'] ) : '';

if ( $class && ( 'wp-block-latest-posts' === $class ) ) {
$is_block = true;
$block_class = 'cptda-block-latest-posts';
Expand Down
2 changes: 2 additions & 0 deletions includes/widgets/class-cptda-widget-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public function form( $instance ) {
/**
* Gets instance settings.
*
* @since 2.6.2
*
* @param array $instance Settings for the current Recent Posts widget instance.
* @return @return array All Recent Posts widget instance settings with back compat applied.
*/
Expand Down
22 changes: 13 additions & 9 deletions includes/widgets/class-cptda-widget-recent-posts.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
/**
* Widget API: WP_Widget_Recent_Posts class
* Recent posts widget
*
* @package WordPress
* @subpackage Widgets
* @since 4.4.0
* @package Custom_Post_Type_Date_Archives
* @subpackage Widget
* @copyright Copyright (c) 2014, Kees Meijer
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 2.4.0
*/

/**
* Core class used to implement a Recent Posts widget.
*
* @since 2.8.0
* @since 2.4.0
*
* @see WP_Widget
*/
Expand All @@ -21,7 +23,7 @@ class CPTDA_Widget_Recent_Posts extends WP_Widget {
/**
* Sets up a new Recent Posts widget instance.
*
* @since 2.8.0
* @since 2.4.0
* @access public
*/
public function __construct() {
Expand All @@ -47,7 +49,7 @@ public function __construct() {
/**
* Outputs the content for the current Recent Posts widget instance.
*
* @since 2.8.0
* @since 2.4.0
* @access public
*
* @param array $args Display arguments including 'before_title', 'after_title',
Expand Down Expand Up @@ -84,7 +86,7 @@ public function widget( $widget_args, $instance ) {
/**
* Handles updating the settings for the current Recent Posts widget instance.
*
* @since 2.8.0
* @since 2.4.0
* @access public
*
* @param array $new_instance New settings for this instance as input by the user via
Expand All @@ -105,7 +107,7 @@ public function update( $new_instance, $old_instance ) {
/**
* Outputs the settings form for the Recent Posts widget.
*
* @since 2.8.0
* @since 2.4.0
* @access public
*
* @param array $instance Current settings.
Expand Down Expand Up @@ -146,6 +148,8 @@ public function form( $instance ) {
*
* Merges instance settings with defaults and applies back compatibility.
*
* @since 2.5.0
*
* @param array $instance Settings for the current Recent Posts widget instance.
* @return @return array All Recent Posts widget instance settings with back compat applied.
*/
Expand Down
17 changes: 13 additions & 4 deletions tests/test-block-recent-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function setUp() {

/**
* Test test recent posts block output.
* @group oo
*/
function test_recent_posts_block_in_post_content() {
global $wp_locale, $wp_version;
Expand Down Expand Up @@ -49,9 +48,6 @@ function test_recent_posts_block_in_post_content() {
);

$recent_posts_html = cptda_get_recent_posts_html( $posts, $args );
// $wp_latest_posts = "<ul class=\"{$block_class}\">\n{$expected}</ul>\n";
// $block_class = $this->get_back_compat_latest_posts_class();


$this->assertEquals( strip_ws( $cptda_latest_posts ), strip_ws( $recent_posts_html ) );
}
Expand Down Expand Up @@ -120,4 +116,17 @@ function test_no_posts_found_message() {
$this->assertEquals( strip_ws( $expected ), strip_ws( $recent_posts_html ) );
}

function test_latest_post_empty_for_non_existing_post_types() {
$this->init( 'cpt' );
$posts = $this->create_posts( 'cpt' );
$this->unregister_post_type( 'cpt' );

$args = cptda_get_recent_posts_settings();
$args['post_type'] = 'cpt';
$query = cptda_get_recent_posts_query( $args );

$recent_posts = cptda_get_recent_posts( $query );
$this->assertEmpty( $recent_posts );
}

}
1 change: 0 additions & 1 deletion tests/test-rest-api-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ function test_rendered_archives_wp_kses() {

/**
* Test test rendered output.
* @group hh
*/
function test_rendered_archives_dropdown() {
global $wp_locale;
Expand Down

0 comments on commit dddad4f

Please sign in to comment.