| 1 |
<?php |
| 2 |
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- core block query; tax filtering required. |
| 3 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
|
| 10 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 11 |
|
| 12 |
class ArchiveList extends Block { |
| 13 |
|
| 14 |
public $tax_query_block = []; |
| 15 |
|
| 16 |
public function get_name() { |
| 17 |
return 'doc-archive-list'; |
| 18 |
} |
| 19 |
|
| 20 |
protected $editor_styles = [ |
| 21 |
'betterdocs-fontawesome', |
| 22 |
'betterdocs-blocks-editor', |
| 23 |
'betterdocs-doc-archive-list', |
| 24 |
'betterdocs-doc_category', |
| 25 |
'betterdocs-category-archive-doc-list', |
| 26 |
'betterdocs-pagination' |
| 27 |
]; |
| 28 |
|
| 29 |
protected $frontend_styles = [ |
| 30 |
'betterdocs-fontawesome', |
| 31 |
'betterdocs-doc-archive-list', |
| 32 |
'betterdocs-doc_category', |
| 33 |
'betterdocs-category-archive-doc-list', |
| 34 |
'betterdocs-pagination' |
| 35 |
]; |
| 36 |
|
| 37 |
public function get_default_attributes() { |
| 38 |
return [ |
| 39 |
'blockId' => '', |
| 40 |
'nested_subcategory' => false, |
| 41 |
'order' => 'asc', |
| 42 |
'orderby' => 'betterdocs_order', |
| 43 |
'layout' => 'layout-1', |
| 44 |
'list_icon' => 'far fa-file-alt', |
| 45 |
'postsPerPageLayoutTwo' => -1, |
| 46 |
'listIconImageUrl' => '', |
| 47 |
'pagination' => false, |
| 48 |
'listTitleTag' => 'h2', |
| 49 |
'listEntryHeadingTag' => 'h3' |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function render( $attributes, $content ) { |
| 54 |
if ( $this->attributes['layout'] == 'layout-2' ) { |
| 55 |
// Layout 2 is handled by Pro version |
| 56 |
add_action( 'archive_handbook_list', [$this, 'render_handbook_view'] ); |
| 57 |
} |
| 58 |
$this->views( 'widgets/block-archive-list' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function render_handbook_view() { |
| 62 |
// This method can be overridden by Pro version |
| 63 |
} |
| 64 |
|
| 65 |
public function view_params() { |
| 66 |
// Sanitize the layout attribute to prevent path traversal (LFI). |
| 67 |
$this->attributes['layout'] = sanitize_file_name( $this->attributes['layout'] ); |
| 68 |
|
| 69 |
global $wp_query; |
| 70 |
$_term_slug = ''; |
| 71 |
if ( isset( $wp_query->query ) && array_key_exists( 'doc_category', $wp_query->query ) ) { |
| 72 |
$_term_slug = $wp_query->query['doc_category']; |
| 73 |
} |
| 74 |
|
| 75 |
if ( isset( $wp_query->query ) && array_key_exists( 'doc_tag', $wp_query->query ) ) { |
| 76 |
$_term_slug = $wp_query->query['doc_tag']; |
| 77 |
add_filter( |
| 78 |
'betterdocs_docs_tax_query_args', |
| 79 |
function ( $tax_query, $_multiple_kb, $_term_slug, $_kb_slug, $_origin_args ) { |
| 80 |
$tax_query[0]['taxonomy'] = 'doc_tag'; |
| 81 |
unset( $tax_query[0]['operator'] ); |
| 82 |
unset( $tax_query[0]['include_children'] ); |
| 83 |
$this->tax_query_block = $tax_query; |
| 84 |
return $tax_query; |
| 85 |
}, |
| 86 |
10, |
| 87 |
5 |
| 88 |
); |
| 89 |
add_filter( |
| 90 |
'betterdocs_articles_args', |
| 91 |
function ( $args, $_term_id, $_origin_args ) { |
| 92 |
if ( empty( $args['tax_query'] ) ) { |
| 93 |
$args['tax_query'] = $this->tax_query_block; |
| 94 |
} |
| 95 |
return $args; |
| 96 |
}, |
| 97 |
10, |
| 98 |
3 |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
$term = ! empty( get_term_by( 'slug', $_term_slug, 'doc_category' ) ) ? get_term_by( 'slug', $_term_slug, 'doc_category' ) : get_term_by( 'slug', $_term_slug, 'doc_tag' ); |
| 103 |
|
| 104 |
// Determine posts per page based on pagination settings |
| 105 |
$posts_per_page = -1; // default: show all posts |
| 106 |
if ( $this->attributes['pagination'] && ( $this->attributes['layout'] == 'layout-1' || $this->attributes['layout'] == 'layout-3' || $this->attributes['layout'] == 'layout-4' ) ) { |
| 107 |
$posts_per_page = isset( $this->attributes['postsPerPageLayoutTwo'] ) && $this->attributes['postsPerPageLayoutTwo'] > 0 |
| 108 |
? $this->attributes['postsPerPageLayoutTwo'] |
| 109 |
: 10; |
| 110 |
} |
| 111 |
|
| 112 |
$_docs_query = [ |
| 113 |
'term_id' => isset( $term->term_id ) ? $term->term_id : 0, |
| 114 |
'orderby' => $this->attributes['orderby'], |
| 115 |
'order' => $this->attributes['order'], |
| 116 |
'kb_slug' => '', |
| 117 |
'posts_per_page' => $posts_per_page, |
| 118 |
'term_slug' => isset( $term->slug ) ? $term->slug : '' |
| 119 |
]; |
| 120 |
|
| 121 |
$default_params = [ |
| 122 |
'term' => $term, |
| 123 |
'nested_subcategory' => (bool) $this->attributes['nested_subcategory'], |
| 124 |
'list_icon_name' => ! empty( $this->attributes['listIconImageUrl'] ) ? [ 'value' => [ 'url' => str_replace( 'blob:', '', $this->attributes['listIconImageUrl'] ) ] ] : ( ! empty( $this->attributes['list_icon'] ) ? [ 'value' => [ 'url' => $this->attributes['list_icon'] ] ] : ( ! empty( betterdocs()->settings->get( 'docs_list_icon' ) ) ? [ 'value' => [ 'url' => betterdocs()->settings->get( 'docs_list_icon' )['url'] ] ] : [] ) ), |
| 125 |
'query_args' => betterdocs()->query->docs_query_args( $_docs_query ), |
| 126 |
'title_tag' => $this->attributes['listEntryHeadingTag'] ?? 'h2', |
| 127 |
'docs_list_title_tag' => $this->attributes['listTitleTag'] ?? 'h2', |
| 128 |
'layout' => $this->attributes['layout'], |
| 129 |
'posts_per_page' => $posts_per_page, |
| 130 |
'list_icon_url' => '', |
| 131 |
'layout_type' => 'block', |
| 132 |
'archive_layout' => $this->attributes['layout'] |
| 133 |
]; |
| 134 |
|
| 135 |
if ( $this->attributes['pagination'] && ( $this->attributes['layout'] == 'layout-1' || $this->attributes['layout'] == 'layout-3' || $this->attributes['layout'] == 'layout-4' ) ) { |
| 136 |
$page = get_query_var( 'paged' ) != '' ? get_query_var( 'paged' ) : 1; |
| 137 |
|
| 138 |
// Use postsPerPageLayoutTwo for layouts 2, 3, and 4 |
| 139 |
$posts_per_page = isset( $this->attributes['postsPerPageLayoutTwo'] ) && $this->attributes['postsPerPageLayoutTwo'] > 0 |
| 140 |
? $this->attributes['postsPerPageLayoutTwo'] |
| 141 |
: 10; |
| 142 |
|
| 143 |
$default_params['query_args']['paged'] = $page; |
| 144 |
$default_params['query_args']['posts_per_page'] = $posts_per_page; |
| 145 |
$default_params['page'] = $page; |
| 146 |
$default_params['posts_per_page'] = $posts_per_page; |
| 147 |
$default_params['pagination'] = $this->attributes['pagination']; |
| 148 |
} |
| 149 |
|
| 150 |
return $default_params; |
| 151 |
} |
| 152 |
} |
| 153 |
|