| 1 |
<?php |
| 2 |
/** |
| 3 |
* Data/options getters used by the widget admin UI and blocks |
| 4 |
* |
| 5 |
* @package Flex Posts |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Get number of layouts |
| 14 |
* |
| 15 |
* @return int |
| 16 |
*/ |
| 17 |
function flex_posts_get_layouts() { |
| 18 |
/** |
| 19 |
* Filter: flex_posts_layouts |
| 20 |
* |
| 21 |
* Filter the number of available layouts. Return an integer. |
| 22 |
* |
| 23 |
* @param int $count Default number of layouts. |
| 24 |
* @return int Modified number of layouts. |
| 25 |
*/ |
| 26 |
return apply_filters( 'flex_posts_layouts', 6 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get layout SVG icons passed from PHP filter. |
| 31 |
* |
| 32 |
* Default SVGs are defined in JavaScript (blocks/list/block.js). |
| 33 |
* This function returns only additional/override SVGs from the PHP filter. |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
function flex_posts_get_layout_svgs() { |
| 38 |
/** |
| 39 |
* Filter: flex_posts_layout_svgs |
| 40 |
* |
| 41 |
* Add or override layout SVG icons. Default SVGs are defined in JavaScript |
| 42 |
* (blocks/list/block.js). Use this filter to add new layouts or override |
| 43 |
* existing SVGs. PHP-provided SVGs take precedence over JS defaults. |
| 44 |
* |
| 45 |
* @param array $svgs Empty array by default. Add SVGs to override or extend. |
| 46 |
* @return array SVG icons to merge with JS defaults. |
| 47 |
* |
| 48 |
* Example: |
| 49 |
* add_filter( 'flex_posts_layout_svgs', function( $svgs ) { |
| 50 |
* // Add custom layout SVG for a new layout 7 |
| 51 |
* $svgs[7] = '<svg viewBox="0 0 80 50" xmlns="http://www.w3.org/2000/svg">...</svg>'; |
| 52 |
* // Override existing layout 1 SVG |
| 53 |
* $svgs[1] = '<svg viewBox="0 0 80 50" xmlns="http://www.w3.org/2000/svg">...</svg>'; |
| 54 |
* return $svgs; |
| 55 |
* } ); |
| 56 |
*/ |
| 57 |
return apply_filters( 'flex_posts_layout_svgs', array() ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get post types |
| 62 |
* |
| 63 |
* @param string $block Block. |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
function flex_posts_get_post_types( $block = true ) { |
| 67 |
$post_types['post'] = __( 'Post', 'flex-posts' ); |
| 68 |
$post_types['page'] = __( 'Page', 'flex-posts' ); |
| 69 |
|
| 70 |
$get_post_types = get_post_types( |
| 71 |
array( |
| 72 |
'public' => true, |
| 73 |
'_builtin' => false, |
| 74 |
), |
| 75 |
'objects' |
| 76 |
); |
| 77 |
|
| 78 |
foreach ( $get_post_types as $post_type ) { |
| 79 |
$post_types[ $post_type->name ] = $post_type->labels->singular_name; |
| 80 |
} |
| 81 |
|
| 82 |
$post_types['any'] = __( 'Any', 'flex-posts' ); |
| 83 |
|
| 84 |
if ( ! $block ) { |
| 85 |
return $post_types; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ( $post_types as $value => $label ) { |
| 89 |
$post_types2[] = array( |
| 90 |
'label' => $label, |
| 91 |
'value' => $value, |
| 92 |
); |
| 93 |
} |
| 94 |
return $post_types2; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get taxonomies |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
function flex_posts_get_taxonomies() { |
| 103 |
$post_types = get_post_types( |
| 104 |
array( |
| 105 |
'public' => true, |
| 106 |
'_builtin' => false, |
| 107 |
), |
| 108 |
'names' |
| 109 |
); |
| 110 |
|
| 111 |
$taxonomies['post'] = get_object_taxonomies( 'post' ); |
| 112 |
$taxonomies['page'] = get_object_taxonomies( 'page' ); |
| 113 |
foreach ( $post_types as $post_type ) { |
| 114 |
$taxonomies[ $post_type ] = get_object_taxonomies( $post_type ); |
| 115 |
} |
| 116 |
return $taxonomies; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get order by data |
| 121 |
* |
| 122 |
* @param string $block Block. |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
function flex_posts_get_order_by( $block = true ) { |
| 126 |
/** |
| 127 |
* Filter: flex_posts_order_by |
| 128 |
* |
| 129 |
* Filter the list of "order by" options available in the widget/block |
| 130 |
* and admin UI. Expected to return an associative array mapping keys |
| 131 |
* to labels. |
| 132 |
* |
| 133 |
* @param array $options Default order-by options. |
| 134 |
* @return array Modified options. |
| 135 |
*/ |
| 136 |
$data = apply_filters( |
| 137 |
'flex_posts_order_by', |
| 138 |
array( |
| 139 |
'newest' => esc_html__( 'Newest', 'flex-posts' ), |
| 140 |
'oldest' => esc_html__( 'Oldest', 'flex-posts' ), |
| 141 |
'comments' => esc_html__( 'Most commented', 'flex-posts' ), |
| 142 |
'title' => esc_html__( 'Alphabetical', 'flex-posts' ), |
| 143 |
'random' => esc_html__( 'Random', 'flex-posts' ), |
| 144 |
'modified' => esc_html__( 'Modified date', 'flex-posts' ), |
| 145 |
'menu_order' => esc_html__( 'Page order', 'flex-posts' ), |
| 146 |
) |
| 147 |
); |
| 148 |
|
| 149 |
if ( ! $block ) { |
| 150 |
return $data; |
| 151 |
} |
| 152 |
|
| 153 |
$data2 = array(); |
| 154 |
foreach ( $data as $value => $label ) { |
| 155 |
$data2[] = array( |
| 156 |
'label' => $label, |
| 157 |
'value' => $value, |
| 158 |
); |
| 159 |
} |
| 160 |
return $data2; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get image sizes data |
| 165 |
* |
| 166 |
* @param string $block Block. |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
function flex_posts_get_image_sizes( $block = true ) { |
| 170 |
$image_sizes[''] = __( 'Default', 'flex-posts' ); |
| 171 |
|
| 172 |
foreach ( get_intermediate_image_sizes() as $size ) { |
| 173 |
$image_sizes[ $size ] = $size; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! $block ) { |
| 177 |
return $image_sizes; |
| 178 |
} |
| 179 |
|
| 180 |
$image_sizes2 = array(); |
| 181 |
foreach ( $image_sizes as $value => $label ) { |
| 182 |
$image_sizes2[] = array( |
| 183 |
'label' => $label, |
| 184 |
'value' => $value, |
| 185 |
); |
| 186 |
} |
| 187 |
return $image_sizes2; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Get html elements allowed for title |
| 192 |
* |
| 193 |
* @return array |
| 194 |
*/ |
| 195 |
function flex_posts_get_title_elements() { |
| 196 |
/** |
| 197 |
* Filter: flex_posts_allowed_title_elements |
| 198 |
* |
| 199 |
* Filter the set of allowed HTML elements that can be used for titles. |
| 200 |
* Expected to return an indexed array of element tags. |
| 201 |
* |
| 202 |
* @param array $elements Default allowed elements. |
| 203 |
* @return array Modified elements. |
| 204 |
*/ |
| 205 |
$elements = apply_filters( |
| 206 |
'flex_posts_allowed_title_elements', |
| 207 |
array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'p', 'span' ) |
| 208 |
); |
| 209 |
return $elements; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get title elements options |
| 214 |
* |
| 215 |
* @param string $block Block. |
| 216 |
* @return array |
| 217 |
*/ |
| 218 |
function flex_posts_get_title_elements_options( $block = true ) { |
| 219 |
$options[''] = __( 'Default', 'flex-posts' ); |
| 220 |
|
| 221 |
foreach ( flex_posts_get_title_elements() as $el ) { |
| 222 |
$options[ $el ] = $el; |
| 223 |
} |
| 224 |
|
| 225 |
if ( ! $block ) { |
| 226 |
return $options; |
| 227 |
} |
| 228 |
|
| 229 |
$options2 = array(); |
| 230 |
foreach ( $options as $value => $label ) { |
| 231 |
$options2[] = array( |
| 232 |
'label' => $label, |
| 233 |
'value' => $value, |
| 234 |
); |
| 235 |
} |
| 236 |
return $options2; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the block category slug used when registering Flex Posts blocks. |
| 241 |
* |
| 242 |
* Defaults to the core "widgets" category. Flex Posts Pro hooks the |
| 243 |
* `flex_posts_block_category` filter to move every Flex Posts block into a |
| 244 |
* dedicated category when the add-on is active. |
| 245 |
* |
| 246 |
* @return string Block category slug. |
| 247 |
*/ |
| 248 |
function flex_posts_get_block_category() { |
| 249 |
/** |
| 250 |
* Filter: flex_posts_block_category |
| 251 |
* |
| 252 |
* @param string $category Block category slug. Default 'widgets'. |
| 253 |
* @return string |
| 254 |
*/ |
| 255 |
return apply_filters( 'flex_posts_block_category', 'widgets' ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Get block attributes |
| 260 |
* |
| 261 |
* @param string $type Block type. |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
function flex_posts_get_attributes( $type = '' ) { |
| 265 |
$attributes = array( |
| 266 |
'title' => array( |
| 267 |
'type' => 'string', |
| 268 |
'default' => '', |
| 269 |
), |
| 270 |
'title_url' => array( |
| 271 |
'type' => 'string', |
| 272 |
'default' => '', |
| 273 |
), |
| 274 |
'title_cat' => array( |
| 275 |
'type' => 'boolean', |
| 276 |
'default' => false, |
| 277 |
), |
| 278 |
'title_url_cat' => array( |
| 279 |
'type' => 'boolean', |
| 280 |
'default' => false, |
| 281 |
), |
| 282 |
'layout' => array( |
| 283 |
'type' => 'number', |
| 284 |
'default' => 1, |
| 285 |
), |
| 286 |
'post_type' => array( |
| 287 |
'type' => 'string', |
| 288 |
'default' => 'post', |
| 289 |
), |
| 290 |
'cat' => array( |
| 291 |
'type' => 'string', |
| 292 |
'default' => '', |
| 293 |
), |
| 294 |
'tag' => array( |
| 295 |
'type' => 'string', |
| 296 |
'default' => '', |
| 297 |
), |
| 298 |
'order_by' => array( |
| 299 |
'type' => 'string', |
| 300 |
'default' => 'newest', |
| 301 |
), |
| 302 |
'number' => array( |
| 303 |
'type' => 'number', |
| 304 |
'default' => 4, |
| 305 |
), |
| 306 |
'skip' => array( |
| 307 |
'type' => 'number', |
| 308 |
'default' => 0, |
| 309 |
), |
| 310 |
'exclude_current' => array( |
| 311 |
'type' => 'boolean', |
| 312 |
'default' => false, |
| 313 |
), |
| 314 |
'show_image' => array( |
| 315 |
'type' => 'string', |
| 316 |
'default' => 'all', |
| 317 |
), |
| 318 |
'image_size' => array( |
| 319 |
'type' => 'string', |
| 320 |
'default' => '', |
| 321 |
), |
| 322 |
'image_size2' => array( |
| 323 |
'type' => 'string', |
| 324 |
'default' => '', |
| 325 |
), |
| 326 |
'show_title' => array( |
| 327 |
'type' => 'boolean', |
| 328 |
'default' => true, |
| 329 |
), |
| 330 |
'show_categories' => array( |
| 331 |
'type' => 'boolean', |
| 332 |
'default' => false, |
| 333 |
), |
| 334 |
'show_author' => array( |
| 335 |
'type' => 'boolean', |
| 336 |
'default' => false, |
| 337 |
), |
| 338 |
'show_avatar' => array( |
| 339 |
'type' => 'boolean', |
| 340 |
'default' => false, |
| 341 |
), |
| 342 |
'show_date' => array( |
| 343 |
'type' => 'boolean', |
| 344 |
'default' => true, |
| 345 |
), |
| 346 |
'show_comments' => array( |
| 347 |
'type' => 'boolean', |
| 348 |
'default' => true, |
| 349 |
), |
| 350 |
'show_excerpt' => array( |
| 351 |
'type' => 'boolean', |
| 352 |
'default' => false, |
| 353 |
), |
| 354 |
'excerpt_length' => array( |
| 355 |
'type' => 'number', |
| 356 |
'default' => 15, |
| 357 |
), |
| 358 |
'show_readmore' => array( |
| 359 |
'type' => 'boolean', |
| 360 |
'default' => false, |
| 361 |
), |
| 362 |
'readmore_text' => array( |
| 363 |
'type' => 'string', |
| 364 |
'default' => '', |
| 365 |
), |
| 366 |
'pagination' => array( |
| 367 |
'type' => 'boolean', |
| 368 |
'default' => false, |
| 369 |
), |
| 370 |
'align' => array( |
| 371 |
'type' => 'string', |
| 372 |
'default' => '', |
| 373 |
), |
| 374 |
'block_title_el' => array( |
| 375 |
'type' => 'string', |
| 376 |
'default' => '', |
| 377 |
), |
| 378 |
'post_title_el' => array( |
| 379 |
'type' => 'string', |
| 380 |
'default' => '', |
| 381 |
), |
| 382 |
'className' => array( |
| 383 |
'type' => 'string', |
| 384 |
'default' => '', |
| 385 |
), |
| 386 |
); |
| 387 |
|
| 388 |
/** |
| 389 |
* Filter: flex_posts_attributes, flex_posts_{type}_attributes |
| 390 |
* |
| 391 |
* Filter the block attributes used when registering the block. Expected |
| 392 |
* to return an associative array conforming to block attribute definitions. |
| 393 |
* |
| 394 |
* @param array $attributes Default attributes array. |
| 395 |
* @return array Modified attributes array. |
| 396 |
* |
| 397 |
* Example: |
| 398 |
* add_filter( 'flex_posts_attributes', function( $attrs ) { |
| 399 |
* $attrs['custom'] = array( 'type' => 'string', 'default' => '' ); |
| 400 |
* return $attrs; |
| 401 |
* } ); |
| 402 |
*/ |
| 403 |
if ( empty( $type ) ) { |
| 404 |
$attributes = apply_filters( 'flex_posts_attributes', $attributes ); |
| 405 |
} else { |
| 406 |
$attributes = apply_filters( 'flex_posts_' . $type . '_attributes', $attributes ); |
| 407 |
} |
| 408 |
return $attributes; |
| 409 |
} |
| 410 |
|