aplb_register_plugin_hooks(); } function aplb_register_plugin_hooks() { add_action( 'init', [ $this, 'aplb_advance_posts_listing_block_callback' ]); add_action( 'rest_api_init', [ $this, 'aplb_register_custom_endpoints' ] ); add_action( 'admin_head', [ $this, 'aplb_get_server_side_data' ]); add_action( 'wp_head', [ $this, 'aplb_get_server_side_data' ]); add_action( 'rest_api_init', [ $this, 'aplb_register_custom_rest_fields' ] ); add_action( 'plugins_loaded', [ $this, 'aplb_load_plugin_languages'] ); } function aplb_load_plugin_languages() { load_plugin_textdomain( 'advanced-posts-listing', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' ); } function aplb_advance_posts_listing_block_callback() { register_block_type(__DIR__ . '/build', array( 'render_callback' => array($this, 'aplb_advance_post_listing_block'), )); } function aplb_advance_post_listing_block($attributes) { $post_type_names = strtolower($attributes['selectedCustomPostType']); if (strpos($post_type_names, 'pages') !== false || strpos($post_type_names, 'posts') !== false) { if (substr($post_type_names, -1) === 's') { $post_type_names = substr($post_type_names, 0, -1); } } if (isset($attributes['sortBy'])) { switch ($attributes['sortBy']) { case 'new-to-old': $order = 'DESC'; $orderby = 'date'; break; case 'old-to-new': $order = 'ASC'; $orderby = 'date'; break; case 'A-Z': $order = 'ASC'; $orderby = 'title'; break; case 'Z-A': $order = 'DESC'; $orderby = 'title'; break; default: $order = 'DESC'; $orderby = 'date'; break; } } else { $orderby = 'date'; $order = 'DESC'; } $args = array( 'posts_per_page' => $attributes['numPostsToShow'], 'post_status' => 'publish', 'post_type' => $post_type_names, 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'order' => $order, 'orderby' => $orderby, ); if (isset($attributes['customTaxonomyPosts']) && !empty($attributes['customTaxonomyPosts'])) { $tax_query = array( 'relation' => 'AND', ); foreach ($attributes['customTaxonomyPosts'] as $key => $terms) { if ($key === 'tags') { $taxonomy = 'post_tag'; } elseif ($key === 'categories') { $taxonomy = 'category'; } else { $taxonomy = $key; } foreach ($terms as $slug) { $tax_query[] = array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $slug, ); } } $args['tax_query'] = $tax_query; } if (isset($tax_query)) { $args['tax_query'] = $tax_query; } $list_items_markup = ''; $args = apply_filters('aplb_frontend_query_args', $args ); $query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); $post_title = get_the_title(); $post_date = get_the_date(); $post_permalink = get_permalink(); $post_author = get_the_author(); $post_categories = get_the_category(); $category_names = array(); if (empty($post_categories)) { $taxonomies = get_object_taxonomies(get_post(), 'names'); foreach ($taxonomies as $taxonomy) { $terms = get_the_terms(get_the_ID(), $taxonomy); if ($terms && !is_wp_error($terms)) { foreach ($terms as $term) { $category_names[] = $term->name; } } } } else { foreach ($post_categories as $category) { $category_names[] = $category->name; } } $category_list = implode(', ', $category_names); $list_items_markup .= '