$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 { protected static $_instance = null; function __construct(){ add_action( 'enqueue_block_assets', [$this, 'enqueue_block_assets'] ); add_action( 'wp_enqueue_scripts', [$this, 'enqueue_assets'] ); if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { add_filter( 'block_categories', [$this, 'categories'] ); } else { add_filter( 'block_categories_all', [$this, 'categories'] ); } add_action( 'wp_loaded', [$this, 'register'] ); add_action( 'rest_api_init', [$this, 'custom_rest'] ); } public static function instance(){ if(self::$_instance === null){ self::$_instance = new self(); } return self::$_instance; } function enqueue_block_assets(){ wp_enqueue_script( 'swiperJS', AP_BLOCK_ASSETS_DIR . 'js/swiper-bundle.min.js', array(), AP_BLOCK_PLUGIN_VERSION, true ); } function enqueue_assets(){ wp_enqueue_style( 'dashicons' ); } function categories($categories){ return array_merge( array( array( 'slug' => 'APBlock', 'title' => 'Advanced Post Block', )), $categories ); } // Categories function register(){ wp_register_script( 'ap_block_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), array( 'wp-blocks', 'wp-element', 'wp-data', 'wp-i18n', 'wp-editor', 'wp-components', 'wp-blob', 'wp-html-entities', 'wp-compose', 'wp-rich-text', 'jquery', 'swiperJS' ), AP_BLOCK_PLUGIN_VERSION, false ); // Backend Script wp_register_style( 'ap_block_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), array( 'wp-edit-blocks' ), AP_BLOCK_PLUGIN_VERSION ); // Backend Style wp_register_script( 'ap_block_script', plugins_url( 'dist/script.js', __FILE__ ), array( 'jquery', 'swiperJS' ), AP_BLOCK_PLUGIN_VERSION, true ); // Frontend Script wp_register_style( 'ap_block_style', plugins_url( 'dist/style.css', __FILE__ ), array( 'wp-editor' ), AP_BLOCK_PLUGIN_VERSION ); // Frontend Style // Register Blocks register_block_type( 'ap-block/posts', array( 'editor_script' => 'ap_block_editor_script', 'editor_style' => 'ap_block_editor_style', 'script' => 'ap_block_script', 'style' => 'ap_block_style', 'render_callback' => [$this, 'render'] ) ); // Translate wp_set_script_translations( 'ap_block_editor_script', 'advanced-post-block', plugin_dir_path( __FILE__ ) . 'languages' ); } // Register function render( $attributes ) { extract( $attributes ); $align = $align ?? 'wide'; $cId = $cId ?? ''; $layout = $layout ?? 'grid'; $columns = $columns ?? array( 'desktop' => 3, 'tablet' => 2, 'mobile' => 1 ); $columnGap = $columnGap ?? 15; $rowGap = $rowGap ?? 15; $isContentEqualHight = $isContentEqualHight ?? true; $sliderHeight = $sliderHeight ?? '350px'; $postType = $postType ?? 'post'; $selectedCategories = $selectedCategories ?? array(); $isPostsPerPageAll = $isPostsPerPageAll ?? false; $postsPerPage = $postsPerPage ?? 12; $postsOrderBy = $postsOrderBy ?? 'date'; $postsOrder = $postsOrder ?? 'desc'; $contentAlign = $contentAlign ?? 'left'; $contentBG = $contentBG ?? array( 'color' => '#f4f2fc' ); $contentPadding = $contentPadding ?? array( 'vertical' => '20px', 'horizontal' => '25px' ); $border = $border ?? array( 'radius' => '5px' ); $sliderIsPage = $sliderIsPage ?? true; $sliderPageColor = $sliderPageColor ?? '#fe6601'; $sliderPageWidth = $sliderPageWidth ?? '15px'; $sliderPageHeight = $sliderPageHeight ?? '15px'; $sliderPageBorder = $sliderPageBorder ?? array( 'radius' => '50%' ); $sliderIsPrevNext = $sliderIsPrevNext ?? true; $sliderPrevNextColor = $sliderPrevNextColor ?? '#fe6601'; $isFImg = $isFImg ?? true; $titleTypo = $titleTypo ?? array( 'fontFamily' => 'Roboto', 'fontSize' => 25, 'googleFontLink' => 'https://fonts.googleapis.com/css2?family=Roboto&display=swap' ); $titleColor = $titleColor ?? '#fe6601'; $titleMargin = $titleMargin ?? array( 'bottom' => '15px' ); $metaTypo = $metaTypo ?? array( 'fontSize' => 13, 'textTransform' => 'uppercase' ); $metaTextColor = $metaTextColor ?? '#333'; $metaLinkColor = $metaLinkColor ?? '#fe6601'; $metaIconColor = $metaIconColor ?? '#fe6601'; $metaMargin = $metaMargin ?? array( 'bottom' => '15px' ); $excerptAlign = $excerptAlign ?? 'justify'; $excerptTypo = $excerptTypo ?? array( 'fontSize' => 15 ); $excerptColor = $excerptColor ?? '#333'; $excerptMargin = $excerptMargin ?? array( 'bottom' => '10px' ); $readMoreAlign = $readMoreAlign ?? 'left'; $readMoreTypo = $readMoreTypo ?? array( 'fontSize' => 14, 'textTransform' => 'uppercase', 'fontWeight' => 600 ); $readMoreColors = $readMoreColors ?? array( 'color' => '#fff', 'bg' => '#fbb040' ); $readMoreHovColors = $readMoreHovColors ?? array( 'color' => '#fff', 'bg' => '#fe6601' ); $readMorePadding = $readMorePadding ?? array( 'vertical' => '12px', 'horizontal' => '35px' ); $readMoreBorder = $readMoreBorder ?? array( 'radius' => '3px'); // Generate Styles $apbPostsStyles = new APBlockStyleGenerator(); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost", array( 'margin-bottom' => 'masonry' === $layout ? $rowGap.'px' : '0px', $border['styles'] ?? 'border-radius: 5px;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPostDefault, #apbAdvancedPosts-$cId .apbPostSideImage", array( 'text-align' => $contentAlign, $contentBG['styles'] ?? 'background-color: #f4f2fc;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostText", array( 'padding' => $contentPadding['styles'] ?? '20px 25px' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPostOverlay .apbPostText", array( $contentBG['styles'] ?? 'background-color: #f4f2fc;' => '', 'align-items' => 'left' === $contentAlign ? 'flex-start' : ('right' === $contentAlign ? 'flex-end' : ('center' === $contentAlign ? 'center' : 'stretch')) )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostTitle", array( 'text-align' => $contentAlign, $titleTypo['styles'] ?? 'font-size: 25px;' => '', 'color' => $titleColor, 'margin' => $titleMargin['styles'] ?? '0 0 15px 0' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostTitle a", array( 'color' => $titleColor )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostMeta", array( 'text-align' => $contentAlign, $metaTypo['styles'] ?? 'font-size: 13px; text-transform: uppercase;' => '', 'color' => $metaTextColor, 'margin' => $metaMargin['styles'] ?? '0 0 15px 0' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostMeta a", array( 'color' => $metaLinkColor )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostMeta .dashicons", array( 'color' => $metaIconColor )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostFImgCats", array( $metaTypo['styles'] ?? 'font-size: 13px; text-transform: uppercase;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostExcerpt", array( 'text-align' => $excerptAlign, $excerptTypo['styles'] ?? 'font-size: 15px;' => '', 'color' => $excerptColor, 'margin' => $excerptMargin['styles'] ?? '0 0 10px 0' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostReadMore", array( 'text-align' => $readMoreAlign )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostReadMore a", array( $readMoreTypo['styles'] ?? 'font-size: 14px; text-transform: uppercase; font-weight: 600;' => '', $readMoreColors['styles'] ?? 'color: #fff; background: #fbb040;' => '', 'padding' => $readMorePadding['styles'] ?? '12px 35px', $readMoreBorder['styles'] ?? 'border-radius: 3px;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbPost .apbPostReadMore a:hover", array( $readMoreHovColors['styles'] ?? 'color: #fff; background: #fe6601;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbGridPosts", array( 'grid-gap' => $rowGap .'px '. $columnGap .'px', 'align-items' => false === $isContentEqualHight ? 'start' : 'initial' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbMasonryPosts", array( 'gap' => $columnGap . 'px' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbSliderPosts, #apbAdvancedPosts-$cId .apbSliderPosts .swiper-slide", array( 'min-height' => $sliderHeight )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbSliderPosts .swiper-pagination .swiper-pagination-bullet", array( 'background' => $sliderPageColor, 'width' => $sliderPageWidth, 'height' => $sliderPageHeight, $sliderPageBorder['styles'] ?? 'border-radius: 50%;' => '' )); $apbPostsStyles::addStyle("#apbAdvancedPosts-$cId .apbSliderPosts .swiper-button-prev, #apbAdvancedPosts-$cId .apbSliderPosts .swiper-button-next", array( 'color' => $sliderPrevNextColor )); // All Posts $posts = get_posts( [ 'post_type' => $postType, 'category' => $selectedCategories, 'posts_per_page' => $isPostsPerPageAll ? -1 : $postsPerPage, 'orderby' => $postsOrderBy, 'order' => $postsOrder ] ); $jsonData = json_encode( array( 'layout' => $layout, 'columns' => $columns, 'columnGap' => $columnGap, 'sliderIsLoop' => $sliderIsLoop ?? true, 'sliderIsTouchMove' => $sliderIsTouchMove ?? false, 'sliderIsAutoplay' => $sliderIsAutoplay ?? true, 'sliderSpeed' => $sliderSpeed ?? 1.5, 'sliderEffect' => $sliderEffect ?? 'slide', 'sliderIsPageClickable' => $sliderIsPageClickable ?? true, 'sliderIsPageDynamic' => $sliderIsPageDynamic ?? true ) ); ob_start(); ?>
' id='apbAdvancedPosts-'>
columns-tablet- columns-mobile-'> foreachPosts( $attributes, $posts ); ?>
cols-tablet- cols-mobile-'> 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(); ?>
ID )) ." target='$tab' rel='noreferrer'>" : ''; ?> ". get_the_category_list(' ', '', $post->ID ) ."" : ''; ?>

ID )) ." target='$tab' rel='noreferrer'>$post->post_title" : $post->post_title; ?>

metaAuthor( $attributes, $post ) . $this->metaDate( $attributes, $post ) . $this->metaCategories( $attributes, $post ) . $this->metaComment( $attributes, $post ); ?>
  ID ); ?>   ID ); ?>   ID ); ?>   ID )->total_comments; ?>
ID ) ), 0, $excerptLength ) ); ?>
true, 'show_in_rest' => true, ), 'objects' ); $options = array(); 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[] = array( 'value' => $post_type->name, 'label' => $post_type->label ); } return $options; } // Post Types function custom_rest() { $post_type = $this->post_types(); foreach ( $post_type as $key => $value ) { register_rest_field( $value['value'], 'wbAuthor', array( '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' => array( 'description' => __( 'Author name and link', 'advanced-post-block' ), 'type' => 'string', ) ) ); register_rest_field( $value['value'], 'wbDate', array( 'get_callback' => function ( $obj ) { return get_the_date( 'M j, Y', $obj['id'] ); }, 'schema' => array( 'description' => __( 'Author name and link', 'advanced-post-block' ), 'type' => 'string', ) ) ); register_rest_field( $value['value'], 'wbCategories', array( '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' => array( 'description' => __( 'Category link lists', 'advanced-post-block' ), 'type' => 'string', ) ) ); register_rest_field( $value['value'], 'wbComment', array( 'get_callback' => function ( $obj ) { return wp_count_comments( $obj['id'] )->total_comments; }, 'schema' => array( 'description' => __( 'Comment', 'advanced-post-block' ), 'type' => 'number', ) ) ); } } // Custom rest } AdvancedPostBlock::instance();