Test Failed
Push — develop ( db5b9c...b55dd4 )
by Paul
17:52
created

RenderCallbackTrait::render_callback()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 139
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
c 1
b 0
f 0
dl 0
loc 139
rs 8.1907
cc 5
nc 4
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * DynamicModule::render_callback()
4
 *
5
 * @package MEE\Modules\DynamicModule
6
 * @since ??
7
 */
8
9
namespace MEE\Modules\DynamicModule\DynamicModuleTrait;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	die( 'Direct access forbidden.' );
13
}
14
15
// phpcs:disable ET.Sniffs.ValidVariableName.UsedPropertyNotSnakeCase -- WP use snakeCase in \WP_Block_Parser_Block
16
17
use ET\Builder\Packages\Module\Module;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Packages\Module\Module was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use ET\Builder\FrontEnd\BlockParser\BlockParserStore;
0 ignored issues
show
Bug introduced by
The type ET\Builder\FrontEnd\BlockParser\BlockParserStore was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use ET\Builder\Framework\Utility\HTMLUtility;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Framework\Utility\HTMLUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use ET\Builder\Packages\Module\Layout\Components\ModuleElements\ModuleElements;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Packages\Modu...Elements\ModuleElements was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use ET\Builder\Packages\Module\Options\Element\ElementComponents;
0 ignored issues
show
Bug introduced by
The type ET\Builder\Packages\Modu...ement\ElementComponents was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
trait RenderCallbackTrait {
24
	use ModuleClassnamesTrait;
25
	use ModuleStylesTrait;
26
	use ModuleScriptDataTrait;
27
28
	/**
29
	 * Dynamic module render callback which outputs server side rendered HTML on the Front-End.
30
	 *
31
	 * @since ??
32
	 *
33
	 * @param array          $attrs    Block attributes that were saved by VB.
34
	 * @param string         $content  Block content.
35
	 * @param \WP_Block      $block    Parsed block object that being rendered.
36
	 * @param ModuleElements $elements ModuleElements instance.
37
	 *
38
	 * @return string HTML rendered of Dynamic module.
39
	 */
40
	public static function render_callback( $attrs, $content, $block, $elements ) {
41
		$post_heading_level = $attrs['postTitle']['decoration']['font']['font']['desktop']['value']['headingLevel'];
42
		$posts_per_page     = $attrs['postItems']['innerContent']['desktop']['value']['postsNumber'];
43
44
		$background_component = ElementComponents::component(
45
			[
46
				'attrs'         => $attrs['module']['decoration'] ?? [],
47
				'id'            => $block->parsed_block['id'],
48
49
				// FE only.
50
				'orderIndex'    => $block->parsed_block['orderIndex'],
51
				'storeInstance' => $block->parsed_block['storeInstance'],
52
			]
53
		);
54
55
		$posts = get_posts(
56
			[
57
				'post_type'      => 'post',
58
				'posts_per_page' => $posts_per_page,
59
			]
60
		);
61
62
		$post_items = '';
63
64
		if ( is_array( $posts ) && count( $posts ) ) {
65
			foreach ( $posts as $post ) {
66
67
				// Post title with link.
68
				$post_title = HTMLUtility::render(
69
					[
70
						'tag'               => 'a',
71
						'attributes'        => [
72
							'href' => get_permalink( $post ),
73
						],
74
						'childrenSanitizer' => 'esc_html',
75
						'children'          => get_the_title( $post ),
76
					]
77
				);
78
79
				// Post title container.
80
				$post_title_container = HTMLUtility::render(
81
					[
82
						'tag'               => $post_heading_level,
83
						'attributes'        => [
84
							'class' => 'example_dynamic_module__post-item-title',
85
						],
86
						'childrenSanitizer' => 'et_core_esc_previously',
87
						'children'          => $post_title,
88
					]
89
				);
90
91
				// Post content.
92
				$post_content = HTMLUtility::render(
93
					[
94
						'tag'               => 'div',
95
						'attributes'        => [
96
							'class' => 'example_dynamic_module__post-item-content',
97
						],
98
						'childrenSanitizer' => 'et_core_esc_previously',
99
						'children'          => get_the_excerpt( $post ),
100
					]
101
				);
102
103
				// Post item.
104
				$post_items .= HTMLUtility::render(
105
					[
106
						'tag'               => 'div',
107
						'attributes'        => [
108
							'class' => 'example_dynamic_module__post-item',
109
						],
110
						'childrenSanitizer' => 'et_core_esc_previously',
111
						'children'          => $post_title_container . $post_content,
112
					]
113
				);
114
			}
115
		}
116
117
		// Title.
118
		$title = $elements->render(
119
			[
120
				'attrName' => 'title',
121
			]
122
		);
123
124
		// Posts container.
125
		if ( ! empty( $post_items ) ) {
126
			$posts_container = HTMLUtility::render(
127
				[
128
					'tag'               => 'div',
129
					'attributes'        => [
130
						'class' => 'example_dynamic_module__post-items',
131
					],
132
					'childrenSanitizer' => 'et_core_esc_previously',
133
					'children'          => $post_items,
134
				]
135
			);
136
		} else {
137
			$posts_container = HTMLUtility::render(
138
				[
139
					'tag'               => 'div',
140
					'childrenSanitizer' => 'esc_html',
141
					'children'          => __( 'No post found.', 'd5-extension-example-modules' ),
142
				]
143
			);
144
		}
145
146
		$parent       = BlockParserStore::get_parent( $block->parsed_block['id'], $block->parsed_block['storeInstance'] );
147
		$parent_attrs = $parent->attrs ?? [];
148
149
		return Module::render(
150
			[
151
				// FE only.
152
				'orderIndex'          => $block->parsed_block['orderIndex'],
153
				'storeInstance'       => $block->parsed_block['storeInstance'],
154
155
				// VB equivalent.
156
				'id'                  => $block->parsed_block['id'],
157
				'name'                => $block->block_type->name,
158
				'moduleCategory'      => $block->block_type->category,
159
				'attrs'               => $attrs,
160
				'elements'            => $elements,
161
				'classnamesFunction'  => [ self::class, 'module_classnames' ],
162
				'stylesComponent'     => [ self::class, 'module_styles' ],
163
				'scriptDataComponent' => [ self::class, 'module_script_data' ],
164
				'parentAttrs'         => $parent_attrs,
165
				'parentId'            => $parent->id ?? '',
166
				'parentName'          => $parent->blockName ?? '',
167
				'children'            => [
168
					$background_component,
169
					HTMLUtility::render(
170
						[
171
							'tag'               => 'div',
172
							'attributes'        => [
173
								'class' => 'example_dynamic_module__inner',
174
							],
175
							'childrenSanitizer' => 'et_core_esc_previously',
176
							'children'          => [
177
								$title,
178
								$posts_container,
179
							],
180
						]
181
					),
182
				],
183
			]
184
		);
185
	}
186
}
187