$style ){ $new = ''; foreach( $style as $property => $value ){ if( $value == '' ){ $new .= $property; }else { $new .= " $property: $value;"; } } $output .= "$selector { $new }"; } return $output; } } // Advanced Post Block class AdvancedPostBlock { function __construct(){ add_action( 'enqueue_block_assets', [$this, 'enqueueBlockAssets'] ); add_action( 'wp_enqueue_scripts', [$this, 'enqueueAssets'] ); if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { add_filter( 'block_categories', [$this, 'blockCategories'] ); } else { add_filter( 'block_categories_all', [$this, 'blockCategories'] ); } add_action( 'wp_loaded', [$this, 'onLoaded'] ); add_action( 'rest_api_init', [$this, 'customRestAPI'] ); add_filter( 'excerpt_more', [$this, 'excerptMore'] ); } function enqueueBlockAssets(){ wp_enqueue_script( 'swiperJS', AP_BLOCK_ASSETS_DIR . 'js/swiper-bundle.min.js', [], '7.0.3', true ); wp_enqueue_script( 'easyTicker', AP_BLOCK_ASSETS_DIR . 'js/easy-ticker.min.js', [], '3.2.1', true ); } function enqueueAssets(){ wp_enqueue_style( 'dashicons' ); } function blockCategories( $categories ){ return array_merge( [ [ 'slug' => 'APBlock', 'title' => 'Advanced Post Block', ] ], $categories ); } // Categories function onLoaded(){ wp_register_style( 'ap-block-posts-editor-style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], AP_BLOCK_PLUGIN_VERSION ); // Backend Style wp_register_style( 'ap-block-posts-style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], AP_BLOCK_PLUGIN_VERSION ); // Frontend Style register_block_type( __DIR__, [ 'editor_style' => 'ap-block-posts-editor-style', 'style' => 'ap-block-posts-style', 'render_callback' => [$this, 'render'] ] ); // Register Block wp_set_script_translations( 'ap-block-posts-editor-script', 'advanced-post-block', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate } // Register function render( $attributes ) { extract( $attributes ); // All Posts $defaultPostFilter = 'post' === $postType ? [ 'category' => $selectedCategories ] : []; $posts = get_posts( array_merge( [ 'post_type' => $postType, 'posts_per_page' => $isPostsPerPageAll ? -1 : $postsPerPage, 'orderby' => $postsOrderBy, 'order' => $postsOrder ], $defaultPostFilter ) ); ob_start(); ?>
' id='apbAdvancedPosts-' data-attributes=''>
columns-tablet- columns-mobile-'> foreachPosts( $attributes, $posts ); ?>
cols-tablet- cols-mobile-'> foreachPosts( $attributes, $posts ); ?>
foreachPosts( $attributes, $posts ); ?>
" : ''; ?>
" : ''; ?>
foreachPosts( $attributes, $posts ); ?>
defaultLayout( $attributes, $post ); } else if ( 'left-image' === $subLayout || 'right-image' === $subLayout ) { echo $this->sideImgLayout( $attributes, $post ); } else if ( 'overlay-content' === $subLayout || 'overlay-content-hover' === $subLayout || 'overlay-box' === $subLayout ) { echo $this->overlayLayout( $attributes, $post ); } else { ?>

postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; ob_start(); ?>
'> postFeatureImg( $attributes, $post ); ?>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $titleMetaFilter; ?>
'> postFeatureImg( $attributes, $post ) : ''; ?>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ); ?>
postFeatureImg( $attributes, $post ) : ''; ?>
ID ); ob_start(); ?>
'>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ); ?> postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; ?>
ID ); $tab = $isLinkNewTab ? '_blank' : '_self'; if( $isFImg && $imgUrl ){ ob_start(); ?>
" : ''; ?> ". get_the_category_list( ' ', '', $post->ID ) ."" : ''; ?>

$post->post_title" : $post->post_title; ?>

metaAuthor( $attributes, $post ) . $this->metaDate( $attributes, $post ) . $this->metaCategories( $attributes, $post ) . $this->metaComment( $attributes, $post ); ?>
    ID ); ?>   ID ); ?>   ID )->total_comments; ?>
ID ), [] ) ), 0, $excerptLength ) ); ?>
true, 'show_in_rest' => true, ], 'objects' ); $options = []; foreach ( $post_types as $post_type ) { if ( 'product' === $post_type->name ) { continue; } if ( 'attachment' === $post_type->name ) { continue; } if ( 'page' === $post_type->name ) { continue; } $options[] = [ 'value' => $post_type->name, 'label' => $post_type->label ]; } return $options; } // Post Types function customRestAPI() { $post_type = $this->post_types(); foreach ( $post_type as $key => $value ) { register_rest_field( $value['value'], 'wbAuthor', [ 'get_callback' => function ( $obj ) { $author['name'] = get_the_author_meta( 'display_name', isset( $obj['author'] ) ? $obj['author'] : '' ); $author['link'] = get_author_posts_url( isset( $obj['author'] ) ? $obj['author'] : '' ); return $author; }, 'schema' => [ 'description' => __( 'Author name and link', 'advanced-post-block' ), 'type' => 'string' ] ] ); register_rest_field( $value['value'], 'wbDate', [ 'get_callback' => function ( $obj ) { return get_the_date( 'M j, Y', $obj['id'] ); }, 'schema' => [ 'description' => __( 'Author name and link', 'advanced-post-block' ), 'type' => 'string' ] ] ); register_rest_field( $value['value'], 'wbCategories', [ 'get_callback' => function ( $obj ) { $catsLink['space'] = get_the_category_list( esc_html__( ' ' ), '', $obj['id'] ); $catsLink['coma'] = get_the_category_list( esc_html__( ', ' ), '', $obj['id'] ); return $catsLink; }, 'schema' => [ 'description' => __( 'Category link lists', 'advanced-post-block' ), 'type' => 'string' ] ] ); register_rest_field( $value['value'], 'wbComment', [ 'get_callback' => function ( $obj ) { return wp_count_comments( $obj['id'] )->total_comments; }, 'schema' => [ 'description' => __( 'Comment', 'advanced-post-block' ), 'type' => 'number' ] ] ); } } // Custom rest function excerptMore( $more ) { return "

". __( 'Read More »', 'b-blocks' ) ."

"; } // Excerpt More } new AdvancedPostBlock;