uninstall_plugin( __FILE__ ); } // Advanced Post Block class APBAdvancedPostBlock{ function __construct(){ add_action( 'enqueue_block_assets', [$this, 'enqueueBlockAssets'] ); add_action( 'admin_enqueue_scripts', [$this, 'adminEnqueueScripts'] ); add_action( 'wp_loaded', [$this, 'onLoaded'] ); add_action( 'rest_api_init', [$this, 'customRestAPI'] ); add_action( 'wp_ajax_apb_get_posts', [$this, 'apbGetPosts'] ); add_action( 'wp_ajax_nopriv_apb_get_posts', [$this, 'apbGetPosts'] ); // add_filter( 'excerpt_more', [$this, 'excerptMore'] ); register_activation_hook( __FILE__, [$this, 'onPluginActivate'] ); if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { add_filter( 'block_categories', [$this, 'blockCategories'] ); } else { add_filter( 'block_categories_all', [$this, 'blockCategories'] ); } } function onPluginActivate(){ if ( is_plugin_active( 'advanced-post-block-pro/plugin.php' ) ){ deactivate_plugins( 'advanced-post-block-pro/plugin.php' ); } } function enqueueBlockAssets(){ wp_register_script( 'swiperJS', APB_ASSETS_DIR . 'js/swiper-bundle.min.js', [], '7.0.3', true ); wp_register_script( 'easyTicker', APB_ASSETS_DIR . 'js/easy-ticker.min.js', [], '3.2.1', true ); wp_localize_script( 'ap-block-posts-script', 'apbPageData', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ) ] ); } function adminEnqueueScripts($hook){ if( 'edit.php' === $hook || 'post.php' === $hook ){ wp_enqueue_style( 'apbAdmin', APB_ASSETS_DIR . 'css/admin.css', [], APB_VERSION ); wp_enqueue_script( 'apbAdmin', APB_ASSETS_DIR . 'js/admin.js', [ 'wp-i18n' ], APB_VERSION, true ); } } function blockCategories( $categories ){ return array_merge( [[ 'slug' => 'APBlock', 'title' => 'Advanced Post Block' ]], $categories ); } // Categories function arrangedPosts ( $posts, $fImgSize = 'full' ) { $arranged = []; foreach( $posts as $post ){ $id = $post->ID; $contentWords = count( preg_split( '/[\s]+/', strip_tags( $post->post_content ) ) ); $arranged[] = [ 'id' => $id, 'link' => get_permalink( $post ), 'name' => $post->post_name, 'thumbnail' => get_the_post_thumbnail_url( $post, $fImgSize ), 'title' => $post->post_title, 'excerpt' => $post->post_excerpt, 'content' => preg_replace('/(<([^>]+)>)/', '', $post->post_content), // Can use strip_tags also 'author' => [ 'name' => get_the_author_meta( 'display_name', $post->post_author ), 'link' => get_author_posts_url( $post->post_author ) ], 'date' => $post->post_date, 'dateGMT' => $post->post_date_gmt, 'modifiedDate' => $post->post_modified, 'modifiedDateGMT' => $post->post_modified_gmt, 'commentCount' => $post->comment_count, 'commentStatus' => $post->comment_status, 'categories' => [ 'coma' => get_the_category_list( esc_html__( ', ' ), '', $id ), 'space' => get_the_category_list( esc_html__( ' ' ), '', $id ) ], 'readTime' => [ 'min' => floor( $contentWords / 200 ), 'sec' => floor( $contentWords % 200 / ( 200 / 60 ) ) ], 'status' => $post->post_status ]; } return $arranged; } function apbGetPosts(){ $cId = sanitize_text_field( $_GET['cId'] ); $pageNumber = (int)sanitize_text_field( $_GET['pageNumber'] ); $postsPerPage = (int)sanitize_text_field( $_GET['postsPerPage'] ); $postsOffset = (int)sanitize_text_field( $_GET['postsOffset'] ) ?? 0; $fImgSize = sanitize_text_field( $_GET['fImgSize'] ); $postsArgs = get_transient("apbPostsArgs-$cId"); $newArgs = wp_parse_args( ['offset' => ( $postsPerPage * ( $pageNumber - 1 ) ) + $postsOffset ], $postsArgs ); $posts = get_posts( $newArgs ); echo wp_json_encode( $this->arrangedPosts( $posts, $fImgSize ) ); wp_die(); } function onLoaded(){ wp_register_style( 'ap-block-posts-style', plugins_url( 'dist/style.css', __FILE__ ), [ 'dashicons' ], APB_VERSION ); // Style wp_register_style( 'ap-block-posts-editor-style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'ap-block-posts-style' ], APB_VERSION ); // Backend 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 remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); // Disable emoji load as image } // Register function render( $attributes ) { extract( $attributes ); $className = $className ?? ''; $blockClassName = 'wp-block-ap-block-posts apbAdvancedPosts ' . $className . ' align' . $align; // All Posts $termsQuery = ['relation' => 'AND']; foreach ($selectedTaxonomies as $taxonomy => $terms){ if( count( $terms ) ){ $termsQuery[] = [ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $terms, ]; } } $defaultPostFilter = 'post' === $postType ? [ 'category__in' => $selectedCategories, 'tag__in' => $selectedTags ?? [] ] : []; $postsArgs = array_merge( [ 'post_type' => $postType, 'posts_per_page' => $isPostsPerPageAll ? -1 : $postsPerPage, 'orderby' => $postsOrderBy, 'order' => $postsOrder, 'tax_query' => $termsQuery, 'offset' => $postsOffset ], $defaultPostFilter ); $allPosts = get_posts( array_merge( $postsArgs, [ 'posts_per_page' => 500 ] ) ); $posts = get_posts( $postsArgs ); set_transient( "apbPostsArgs-$cId", $postsArgs, 60*60*24 ); ob_start(); ?>
count( $allPosts ) ] ) ); ?>' >
columns-tablet- columns-mobile-'> foreachPosts( $attributes, $posts ); ?>
foreachPosts( $attributes, $posts ); ?>
cols-tablet- cols-mobile-'> foreachPosts( $attributes, $posts ); ?>
foreachPosts( $attributes, $posts, 'swiper-slide' ); ?>
" : ''; ?>
" : ''; ?>
foreachPosts( $attributes, $posts ); ?>
defaultLayout( $attributes, $post, $layoutClass ); } else if ( 'left-image' === $subLayout || 'right-image' === $subLayout ) { echo $this->sideImgLayout( $attributes, $post, $layoutClass ); } else if ( 'overlay-content' === $subLayout || 'overlay-content-hover' === $subLayout || 'overlay-box' === $subLayout || 'overlay-content-box' === $subLayout || 'overlay-half-content' === $subLayout ) { echo $this->overlayLayout( $attributes, $post, $layoutClass ); } else { ?>

'auto' ]; $imgUrl = get_the_post_thumbnail_url( $post, $fImgSize ); $titleMetaFilter = 'title-meta' !== $subLayout ? $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; ob_start(); ?>
ContentHeight apbPostDefault '> postFeatureImg( $attributes, $post ); ?>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $titleMetaFilter; ?>
'auto' ]; $fImgSize = $fImgSize ?? 'full'; $imgUrl = get_the_post_thumbnail_url( $post, $fImgSize ); ob_start(); ?>
ContentHeight apbPostSideImage '> postFeatureImg( $attributes, $post ) : ''; ?>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ) . $this->postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ); ?>
postFeatureImg( $attributes, $post ) : ''; ?>
'auto' ]; $fImgSize = $fImgSize ?? 'full'; $imgEl = get_the_post_thumbnail( $post, $fImgSize ); ob_start(); ?>
ContentHeight apbPostOverlay '>
postTitle( $attributes, $post ) . $this->postMetaData( $attributes, $post ); ?> postExcerpt( $attributes, $post ) . $this->postReadMore( $attributes, $post ) : ''; ?>
" : ''; ?> ". get_the_category_list( ' ', '', $post->ID ) ."" : ''; ?>

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

metaAuthor( $attributes, $post ) . $this->metaDate( $attributes, $post ) . $this->metaCategories( $attributes, $post ) . $this->metaReadingTime( $attributes, $post ) . $this->metaComment( $attributes, $post ); ?>
post_author ); $authorName = get_the_author_meta( 'display_name', $post->post_author ); $iconEl = $metaAuthorIcon ? "Author" : ""; $authorEl = false === $isMetaAuthorLink ? "$authorName" : ""; $metaAuthorEl = "$iconEl $authorEl"; ob_start(); echo wp_kses_post( $metaAuthorEl ); return ob_get_clean(); } else { return ''; } } // Meta Author function metaDate( $attributes, $post ) { extract( $attributes ); $metaDateFormat = $metaDateFormat ?? 'M j, Y'; if ( $isMetaDate ) { $date = get_the_date( $metaDateFormat, $post->ID ); $iconEl = $metaDateIcon ? "Author" : ""; $metaDateEl = "$iconEl $date"; ob_start(); echo wp_kses_post( $metaDateEl ); return ob_get_clean(); } else { return ''; } } // Meta Date function metaCategories( $attributes, $post ) { extract( $attributes ); if ( $isMetaCategory && 'content' === $metaCategoryIn ) { $categoryList = get_the_category_list( esc_html__( ', ' ), '', $post->ID ); $iconEl = $metaCategoryIcon ? "Category" : ""; $metaCategoryEl = "$iconEl $categoryList"; ob_start(); echo wp_kses_post( $metaCategoryEl ); return ob_get_clean(); } else { return ''; } } // Meta Categories function metaReadingTime( $attributes, $post ) { extract( $attributes ); if ( $isMetaReadTime ) { $contentWords = count( preg_split( '/[\s]+/', strip_tags( $post->post_content ) ) ); $readTimeMin = floor( $contentWords / 200 ); $readTimeSec = floor( $contentWords % 200 / ( 200 / 60 ) ); $readTime = $isMetaReadTimeSec ? $readTimeMin .':'. $readTimeSec : $readTimeMin; $iconEl = $metaReadTimeIcon ? "Reding Time" : ""; $readTimeContent = $readTime .' '. $metaReadTimeLabel; $metaReadTimeEl = "$iconEl $readTimeContent"; ob_start(); echo wp_kses_post( $metaReadTimeEl ); return ob_get_clean(); } else { return ''; } } // Meta Categories function metaComment( $attributes, $post ) { extract( $attributes ); if ( $isMetaComment ) { $commentUrl = esc_url( get_permalink( $post ) ) . '/#comments'; $commentCount = wp_count_comments( $post->ID )->total_comments; $iconEl = $metaCommentIcon ? "Comment" : ""; $metaCommentEl = "$iconEl $commentCount"; ob_start(); echo wp_kses_post( $metaCommentEl ); return ob_get_clean(); } else { return ''; } } // Meta Comment function postExcerpt( $attributes, $post ) { extract( $attributes ); $excerpt = $post->post_excerpt; $content = preg_replace('/(<([^>]+)>)/', '', $post->post_content); $excerptContent = ( !$isExcerptFromContent && $excerpt ) ? $excerpt : $content; if ( $isExcerpt ) { ob_start(); ?>

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' ] ] ); register_rest_field( $value['value'], 'wbReadTime', [ 'get_callback' => function ( $obj ) { $contentWords = count( preg_split( '/[\s]+/', strip_tags( $obj['content']['rendered'] ) ) ); return [ 'min' => floor( $contentWords / 200 ), 'sec' => floor( $contentWords % 200 / ( 200 / 60 ) ) ]; }, 'schema' => [ 'description' => __( 'Reading Time', 'advanced-post-block' ), 'type' => 'number' ] ] ); } } // Custom rest function excerptMore( $more ) { return "

". __( 'Read More »', 'advanced-post-block' ) ."

"; } // Excerpt More } new APBAdvancedPostBlock; // Require Files global $apb_bs; require_once plugin_dir_path( __FILE__ ) . '/inc/style.php'; if( $apb_bs->can_use_premium_feature() ) { require_once plugin_dir_path( __FILE__ ) . '/inc/custom-post.php'; }