FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-challenge/php/Block.php at develop · manuelRod/coding-challenge · GitHub
manuelRod
/
coding-challenge
Public
forked from
Emerald2013/coding-challenge
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-challenge
/
php
/
Block.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
220 lines (207 loc) · 5.8 KB
Breadcrumbs
coding-challenge
/
php
/
Block.php
Copy path
File metadata and controls
220 lines (207 loc) · 5.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Block class.
*
* @package SiteCounts
*/
namespace
XWP
\
SiteCounts
;
use
WP_Block
;
use
WP_Query
;
/**
* The Site Counts dynamic block.
*
* Registers and renders the dynamic block.
*/
class
Block {
/**
* The Plugin instance.
*
* @var Plugin
*/
protected
$
plugin
;
/**
* Instantiates the class.
*
* @param Plugin $plugin The plugin object.
*/
public
function
__construct
(
$
plugin
) {
$
this
->
plugin
=
$
plugin
;
}
/**
* Adds the action to register the block.
*
* @return void
*/
public
function
init
() {
add_action
(
'
init
'
, [
$
this
,
'
register_block
'
] );
}
/**
* Registers the block.
*/
public
function
register_block
() {
register_block_type_from_metadata
(
$
this
->
plugin
->
dir
(),
[
'
render_callback
'
=> [
$
this
,
'
render_callback
'
],
]
);
}
/**
* Renders the block.
*
* @param array $attributes The attributes for the block.
* @param string $content The block content, if any.
* @param WP_Block $block The instance of this block.
* @return string The markup of the block.
*/
public
function
render_callback
(
$
attributes
,
$
content
,
$
block
) {
$
class_name
=
$
attributes
[
'
className
'
];
$
post_id
=
get_the_ID
();
ob_start
();
?>
<div class="
<?php
echo
esc_attr
(
$
class_name
);
?>
">
<h2>Post Counts</h2>
<ul>
<?php
foreach
(
$
this
->
get_post_type_counter
()
as
$
post_label
=>
$
post_count
) :
?>
<li>
<?php
echo
esc_html
(
"
There are
$
post_count
$
post_label
.
"
);
?>
</li>
<?php
endforeach
;
?>
</ul>
<p>
<?php
echo
esc_html
(
"
The current post ID is
$
post_id
.
"
);
?>
</p>
<?php
/**
* For simplicity, and lacking context of where this would be used, i will limit the query to 30 results.
* Understanding that they will be enough to get at least 5 matches.
* In a real world scenario, this strategy should be revised and probably have a fallback in case not enough
* results are retrieved.
*/
?>
<?php
$
baz_posts
=
$
this
->
get_posts_by_category
(
'
baz
'
,
30
);
?>
<?php
/**
* To avoid a multi-dimensional query, we filter in PHP tag and post__not_in.
*/
?>
<?php
$
foo_posts
=
$
this
->
filter_tag_posts
(
$
baz_posts
,
'
foo
'
,
$
post_id
);
?>
<?php
if
( !
empty
(
$
foo_posts
) ) :
?>
<?php
/**
* As in this h2, doesn't mention the need of meta_value == 'Accepted' I decided to ignore it.
* If it would be needed, and as it is not advise to look by meta_value for its non performance.
* Depending on the needs and specifications of the project we would need to fine the most convinient strategy.
* For instance, creating an index:
* ALTER TABLE `wp_postmeta` ADD INDEX `wp_postmeta_idx_meta_key_meta_value_post_id` (`meta_key` (255),`meta_value` (255), `post_id`);
*/
?>
<h2>Any 5 posts with the tag of foo and the category of baz</h2>
<ul>
<?php
foreach
(
$
foo_posts
as
$
post_id
) :
?>
<li>
<?php
echo
esc_html
(
get_the_title
(
$
post_id
) );
?>
</li>
<?php
endforeach
;
?>
</ul>
<?php
endif
;
?>
</div>
<?php
return
ob_get_clean
();
}
/**
* Gets post and pages belonging to category $category_name.
*
* @param string $category_name Category name to query.
* @param int $posts_per_page Posts per page.
*
* @return WP_Query
*/
protected
function
get_posts_by_category
(
$
category_name
,
$
posts_per_page
) {
$
query
=
new
WP_Query
(
[
'
post_type
'
=> [
'
post
'
,
'
page
'
],
'
post_status
'
=>
'
any
'
,
'
fields
'
=>
'
ids
'
,
'
date_query
'
=> [
[
'
hour
'
=>
9
,
'
compare
'
=>
'
>=
'
,
],
[
'
hour
'
=>
17
,
'
compare
'
=>
'
<=
'
,
],
],
'
tax_query
'
=> [
[
'
taxonomy
'
=>
'
category
'
,
'
field
'
=>
'
name
'
,
'
terms
'
=>
$
category_name
,
'
include_children
'
=>
false
,
],
],
'
no_found_rows
'
=>
true
,
'
update_post_meta_cache
'
=>
false
,
'
posts_per_page
'
=>
$
posts_per_page
,
]
);
return
$
query
;
}
/**
* Creates an array that will count number of posts per post type.
*
* @return array Associative array with 'label name' => number of posts.
*/
protected
function
get_post_type_counter
() {
$
post_types
=
get_post_types
( [
'
public
'
=>
true
] );
$
counter
= [];
foreach
(
$
post_types
as
$
post_type_slug
) {
$
status
= (
'
attachment
'
===
$
post_type_slug
) ?
'
inherit
'
:
'
publish
'
;
$
post_count
=
wp_count_posts
(
$
post_type_slug
)->
$
status
;
$
post_type_object
=
get_post_type_object
(
$
post_type_slug
);
$
counter
[
$
post_type_object
->
labels
->
name
] =
$
post_count
;
}
return
$
counter
;
}
/**
* Given a Query result and a tag name, filter posts that has $tag_name associated.
* We also mock the WP_Query functionality $post__not_in.
*
* @param WP_Query $query WP_Query result where we will filter from.
* @param string $tag_name Tag Name to filter.
* @param int $post__not_in Specify post NOT to retrieve.
*
* @return array
*/
protected
function
filter_tag_posts
(
WP_Query
$
query
,
$
tag_name
,
$
post__not_in
) {
$
post_ids
= [];
if
(
$
query
->
have_posts
() ) {
foreach
(
$
query
->
posts
as
$
post_id
) {
if
( !
$
this
->
post_has_tag
(
$
post_id
,
$
tag_name
) ) {
continue
;
}
if
(
$
post_id
===
$
post__not_in
) {
continue
;
}
$
post_ids
[] =
$
post_id
;
}
}
return
$
post_ids
;
}
/**
* Returns if a given post_id has $tag_name tag associated.
*
* @param int $post_id Post_id to look up.
* @param string $tag_name Tag name to be looked up.
*
* @return bool
*/
protected
function
post_has_tag
(
$
post_id
,
$
tag_name
) {
$
tags
=
get_the_tags
(
$
post_id
);
foreach
(
$
tags
as
$
tag
) {
if
(
$
tag
->
name
===
$
tag_name
) {
return
true
;
}
}
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL