← All changes
|
includes/Editors/BlockEditor/Blocks/ArchiveList.php
+134
-67
3.4.2
→
4.9.3
View file →
| @@ -1,85 +1,152 @@ | ||
| 1 | 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; | |
| 2 | 4 | |
| 3 | -namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks; | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 4 | 8 | |
| 9 | + | |
| 5 | 10 | use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 6 | 11 | |
| 7 | 12 | class ArchiveList extends Block { |
| 8 | 13 | |
| 9 | - public $tax_query_block = []; | |
| 14 | + public $tax_query_block = []; | |
| 10 | 15 | |
| 11 | - public function get_name() { | |
| 12 | - return 'doc-archive-list'; | |
| 13 | - } | |
| 16 | + public function get_name() { | |
| 17 | + return 'doc-archive-list'; | |
| 18 | + } | |
| 14 | 19 | |
| 15 | - protected $editor_styles = [ | |
| 16 | - 'betterdocs-fontawesome', | |
| 17 | - 'betterdocs-blocks-editor', | |
| 18 | - 'betterdocs-doc-archive-list', | |
| 19 | - 'betterdocs-doc_category' | |
| 20 | - ]; | |
| 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 | + ]; | |
| 21 | 28 | |
| 22 | - protected $frontend_styles = [ | |
| 23 | - 'betterdocs-fontawesome', | |
| 24 | - 'betterdocs-doc-archive-list', | |
| 25 | - 'betterdocs-doc_category' | |
| 26 | - ]; | |
| 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 | + ]; | |
| 27 | 36 | |
| 28 | - public function get_default_attributes() { | |
| 29 | - return [ | |
| 30 | - 'blockId' => '', | |
| 31 | - 'nested_subcategory' => false, | |
| 32 | - 'order' => 'asc', | |
| 33 | - 'orderby' => 'title' | |
| 34 | - ]; | |
| 35 | - } | |
| 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 | + } | |
| 36 | 52 | |
| 37 | - public function render( $attributes, $content ) { | |
| 38 | - $this->views( 'widgets/block-archive-list' ); | |
| 39 | - } | |
| 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 | + } | |
| 40 | 60 | |
| 41 | - public function view_params() { | |
| 42 | - global $wp_query; | |
| 43 | - $_term_slug = ''; | |
| 44 | - if ( isset( $wp_query->query ) && array_key_exists( 'doc_category', $wp_query->query ) ) { | |
| 45 | - $_term_slug = $wp_query->query['doc_category']; | |
| 46 | - } | |
| 61 | + public function render_handbook_view() { | |
| 62 | + // This method can be overridden by Pro version | |
| 63 | + } | |
| 47 | 64 | |
| 48 | - if ( isset( $wp_query->query ) && array_key_exists( 'doc_tag', $wp_query->query ) ) { | |
| 49 | - $_term_slug = $wp_query->query['doc_tag']; | |
| 50 | - add_filter( 'betterdocs_docs_tax_query_args', function ( $tax_query, $_multiple_kb, $_term_slug, $_kb_slug, $_origin_args ) { | |
| 51 | - $tax_query[0]['taxonomy'] = 'doc_tag'; | |
| 52 | - unset( $tax_query[0]['operator'] ); | |
| 53 | - unset( $tax_query[0]['include_children'] ); | |
| 54 | - $this->tax_query_block = $tax_query; | |
| 55 | - return $tax_query; | |
| 56 | - }, 10, 5 ); | |
| 57 | - add_filter( 'betterdocs_articles_args', function ( $args, $_term_id, $_origin_args ) { | |
| 58 | - if( empty( $args['tax_query'] ) ) { | |
| 59 | - $args['tax_query'] = $this->tax_query_block; | |
| 60 | - } | |
| 61 | - return $args; | |
| 62 | - }, 10, 3); | |
| 63 | - } | |
| 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'] ); | |
| 64 | 68 | |
| 65 | - $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' ); | |
| 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 | + } | |
| 66 | 74 | |
| 67 | - $_docs_query = [ | |
| 68 | - 'term_id' => isset( $term->term_id ) ? $term->term_id : 0, | |
| 69 | - 'orderby' => $this->attributes['orderby'], | |
| 70 | - 'order' => $this->attributes['order'], | |
| 71 | - 'postsOrderBy' => $this->attributes['orderby'], | |
| 72 | - 'postsOrder' => $this->attributes['order'], | |
| 73 | - 'kb_slug' => '', | |
| 74 | - 'posts_per_page' => $term == false ? 5 : -1, | |
| 75 | - 'term_slug' => isset( $term->slug ) ? $term->slug : '' | |
| 76 | - ]; | |
| 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 | + } | |
| 77 | 101 | |
| 78 | - return [ | |
| 79 | - 'term' => $term, | |
| 80 | - 'nested_subcategory' => (bool) $this->attributes['nested_subcategory'], | |
| 81 | - 'query_args' => betterdocs()->query->docs_query_args( $_docs_query ), | |
| 82 | - 'title_tag' => 'h2' | |
| 83 | - ]; | |
| 84 | - } | |
| 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 | + } | |
| 85 | 152 | } |