renderer = $renderer; } public function init() { if ( ! AIT_AIRCE_Settings::get( 'enable_shortcode_block' ) ) { return; } add_shortcode( 'ait_airce_related', [ $this, 'shortcode' ] ); } public function shortcode( $atts = [] ) { $atts = shortcode_atts( [ 'post_id' => 0, 'post_type' => '', 'count' => 0, 'label' => '', 'show_thumbnails' => '', 'location' => '', 'context_key' => 'default', ], (array) $atts, 'ait_airce_related' ); $post_id = intval( $atts['post_id'] ); if ( $post_id <= 0 ) { $post_id = get_the_ID(); } // If the shortcode is used outside the loop, get_the_ID() can be 0. // In that case, pick the most recent post of the configured default type. if ( $post_id <= 0 ) { $fallback_type = sanitize_key( (string) $atts['post_type'] ); if ( '' === $fallback_type ) { $fallback_type = sanitize_key( (string) AIT_AIRCE_Settings::get( 'shortcode_default_post_type' ) ); } // Free version only supports posts and pages by default. if ( ! in_array( $fallback_type, [ 'post', 'page' ], true ) ) { $fallback_type = 'post'; } $q = new WP_Query( [ 'post_type' => $fallback_type, 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC', 'fields' => 'ids', 'no_found_rows' => true, ] ); if ( ! empty( $q->posts ) ) { $post_id = intval( $q->posts[0] ); } } if ( $post_id <= 0 ) { return ''; } $count = intval( $atts['count'] ); if ( $count <= 0 ) { $count = intval( AIT_AIRCE_Settings::get( 'related_count' ) ); } $label = $atts['label']; if ( '' === $label ) { $label = strval( AIT_AIRCE_Settings::get( 'related_label' ) ); } $show_thumbnails = $atts['show_thumbnails']; if ( '' === $show_thumbnails ) { $show_thumbnails = (bool) AIT_AIRCE_Settings::get( 'show_thumbnails' ); } else { $show_thumbnails = ( $show_thumbnails === '1' || $show_thumbnails === 'true' ); } $location = (string) $atts['location']; if ( '' === $location ) { $location = 'shortcode'; } return $this->renderer->render( $post_id, [ 'context_key' => sanitize_key( (string) $atts['context_key'] ), 'location' => sanitize_key( $location ), 'count' => $count, 'label' => $label, 'show_thumbnails' => $show_thumbnails, ] ); } }