$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, '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, 'register'] ); add_action( 'rest_api_init', [$this, 'customRestAPI'] ); add_filter( 'excerpt_more', [$this, 'excerptMore'] ); } public static function instance(){ if( self::$_instance === null ){ self::$_instance = new self(); } return self::$_instance; } 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 register(){ 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 ); // Generate Styles $apbPostsStyles = new APBlockStyleGenerator(); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost", [ 'margin-bottom' => 'masonry' === $layout ? $rowGap.'px' : '0px', $border['styles'] ?? 'border-radius: 5px;' => '' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPostDefault, #apbAdvancedPosts-$cId .apbPostSideImage", [ 'text-align' => $contentAlign, $contentBG['styles'] ?? 'background-color: #f4f2fc;' => '' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostText", [ 'padding' => $contentPadding['styles'] ?? '20px 25px' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPostOverlay .apbPostText", [ $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", [ 'text-align' => $contentAlign, $titleTypo['styles'] ?? 'font-size: 25px;' => '', 'color' => $titleColor, 'margin' => $titleMargin['styles'] ?? '0 0 15px 0' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostTitle a", [ 'color' => $titleColor ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostMeta", [ '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", [ 'color' => $metaLinkColor ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostMeta .dashicons", [ 'color' => $metaIconColor ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostFImgCats", [ $metaTypo['styles'] ?? 'font-size: 13px; text-transform: uppercase;' => '' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostFImgCats a", [ $metaColorsOnImage['styles'] ?? 'color: #fff; background: #4527a4;' => '', ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostExcerpt", [ 'text-align' => $excerptAlign, $excerptTypo['styles'] ?? 'font-size: 15px;' => '', 'color' => $excerptColor, 'margin' => $excerptMargin['styles'] ?? '0 0 10px 0' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostReadMore", [ 'text-align' => $readMoreAlign ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostReadMore a", [ $readMoreTypo['styles'] ?? 'font-size: 14px; text-transform: uppercase; font-weight: 600;' => '', $readMoreColors['styles'] ?? 'color: #fff; background: #8344c5;' => '', 'padding' => $readMorePadding['styles'] ?? '12px 35px', $readMoreBorder['styles'] ?? 'border-radius: 3px;' => '' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbPost .apbPostReadMore a:hover", [ $readMoreHovColors['styles'] ?? 'color: #fff; background: #4527a4;' => '' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbGridPosts", [ 'grid-gap' => $rowGap .'px '. $columnGap .'px', 'align-items' => false === $isContentEqualHight ? 'start' : 'initial' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbMasonryPosts", [ 'gap' => $columnGap . 'px' ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbSliderPosts, #apbAdvancedPosts-$cId .apbSliderPosts .swiper-slide", [ 'min-height' => $sliderHeight ] ); $apbPostsStyles::addStyle( "#apbAdvancedPosts-$cId .apbSliderPosts .swiper-pagination .swiper-pagination-bullet", [ '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", [ 'color' => $sliderPrevNextColor ] ); // 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 ) ); $sliderJsonData = wp_json_encode( [ 'layout' => $layout, 'columns' => $columns, 'columnGap' => $columnGap, 'sliderIsLoop' => $sliderIsLoop, 'sliderIsTouchMove' => $sliderIsTouchMove, 'sliderIsAutoplay' => $sliderIsAutoplay, 'sliderSpeed' => $sliderSpeed, 'sliderEffect' => $sliderEffect, 'sliderIsPageClickable' => $sliderIsPageClickable, 'sliderIsPageDynamic' => $sliderIsPageDynamic ] ); ob_start(); ?>