-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-stats.php
More file actions
176 lines (147 loc) · 4.34 KB
/
class-stats.php
File metadata and controls
176 lines (147 loc) · 4.34 KB
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
<?php
/**
* Statistics implementation
*
* @package Which_Blocks
*/
namespace WhichBlocks;
use WP_Block_Type_Registry;
/**
* Statistics class
*/
class Stats {
/**
* Get usage of blocks
*
* @param array $args Search arguments.
* @return array
*/
public static function get_usage( $args = array() ) {
global $wpdb;
$args = self::prepare_args( $args );
if ( ! is_array( $args['blocks'] ) || ! count( $args['blocks'] ) ) {
return array();
}
$sql = self::prepare_sql( $args );
$results = $wpdb->get_results( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared on previous steps.
$all_blocks = array_filter(
$results,
function( $item ) {
return ! is_null( $item->block_name );
}
);
/**
* Filter the results of which blocks get usage
*
* @param array $all_blocks Resulting array with all blocks and count of posts containing each block.
* @return array
*/
return apply_filters( 'which_blocks_get_usage', $all_blocks );
}
/**
* Prepare where clauses
*
* @param array $args Arguments.
* @return array
*/
public static function where_clauses( $args ) {
global $wpdb;
$where_clauses = array();
if ( isset( $args['post_type'] ) && is_array( $args['post_type'] ) && count( $args['post_type'] ) ) {
$where_clauses[] = $wpdb->prepare( 'post_type IN (' . implode( ',', array_fill( 0, count( $args['post_type'] ), '%s' ) ) . ')', $args['post_type'] );
}
if ( isset( $args['post_status'] ) && is_array( $args['post_status'] ) && count( $args['post_status'] ) ) {
$where_clauses[] = $wpdb->prepare( 'post_status IN (' . implode( ',', array_fill( 0, count( $args['post_status'] ), '%s' ) ) . ')', $args['post_status'] );
}
return $where_clauses;
}
/**
* Prepare SQL
*
* @param array $args Arguments.
* @return string
*/
public static function prepare_sql( $args ) {
global $wpdb;
$where_clauses = self::where_clauses( $args );
if ( count( $where_clauses ) ) {
$where = join( ' AND ', $where_clauses );
} else {
$where = '';
}
$queries = array();
foreach ( $args['blocks'] as $block ) {
if ( 'core/' === substr( $block, 0, 5 ) ) {
$block_name = substr( $block, 5 );
} else {
$block_name = $block;
}
$search_pattern = '<!-- wp:' . $block_name . ' ';
$block_where = $wpdb->prepare( 'WHERE post_content LIKE %s', '%' . $wpdb->esc_like( $search_pattern ) . '%' );
if ( $where ) {
$block_where .= ' AND ' . $where;
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $block_where is prepared.
$queries[] = $wpdb->prepare( "(SELECT %s as block_name, COUNT(*) as cnt FROM {$wpdb->posts} " . $block_where . ' GROUP BY %s)', $block, $block );
}
return join( ' UNION ', $queries ) . ' ORDER BY cnt DESC';
}
/**
* Prepare arguments for get_usage
*
* @param array $args Search arguments.
* @return array
*/
public static function prepare_args( $args ) {
$args = wp_parse_args(
$args,
array(
'post_type' => array( 'post', 'page' ),
'post_status' => array( 'publish' ),
'blocks' => 'any',
'orderby' => 'cnt',
'order' => 'DESC',
)
);
if ( 'any' === $args['post_type'] ) {
$args['post_type'] = array();
} elseif ( ! is_array( $args['post_type'] ) ) {
$args['post_type'] = array( $args['post_type'] );
}
if ( 'any' === $args['post_status'] ) {
$args['post_status'] = array();
} elseif ( ! is_array( $args['post_status'] ) ) {
$args['post_status'] = array( $args['post_status'] );
}
if ( 'any' === $args['blocks'] ) {
$args['blocks'] = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );
} elseif ( is_array( $args['blocks'] ) ) {
$args['blocks'] = $args['blocks'];
} elseif ( is_string( $args['blocks'] ) ) {
$args['blocks'] = array( $args['blocks'] );
}
/**
* Filter wich blocks get usage arguments before building the SQL
*
* @param array $args Arguments array.
* @return array
*/
return apply_filters( 'which_blocks_get_usage_args', $args );
}
/**
* Sort the block statistics elements
*
* @param array $a First operand.
* @param array $b Second operand.
* @return int
*/
public static function sort( $a, $b ) {
$sum_a = array_sum( $a['count'] );
$sum_b = array_sum( $b['count'] );
if ( $sum_a === $sum_b ) {
return 0;
} else {
return $sum_a > $sum_b ? -1 : 1;
}
}
}