This class aims to facilitate the process of obtaining and sanitizing data from the post information (ID or object).
With MYC Getter, is possible to replace this:
$posts = [];
foreach ($query->posts as $post) {
$genresObjects = get_the_terms($post, 'genre');
$genres = [];
if (is_array($genresObjects)) {
foreach ($genresObjects as $genre) {
$genres[] = [
'name' => esc_html($genre->name),
'url' => esc_url(get_term_link($genre)),
];
}
}
$posts[] = [
'title' => esc_html($post->post_title),
'url' => esc_url(get_the_permalink($post)),
'image' => get_the_post_thumbnail($post, 'large'),
'author' => absint(get_field('author', $post)),
'genres' => $genres,
];
}By this:
$args = [
'acf' => ['author' => 'string'],
'taxonomies' => ['genre' => 'link'],
];
$getter = new MycGetter($args);
$posts = [];
foreach ($query->posts as $post) {
$posts[] = $getter->escapedContent($post);
}- PHP >= 8.0
composer require radishlab/myc-getteruse RadishLab\MycGetter\MycGetter;
class MyClass {
public function MyMethod()
{
$getter = new MycGetter();
}
}The class accept two parameters:
$args- An array of arguments, listing the content you want to pull, or a preset name
- If nothing is passed, the class will use the arguments of the preset
default
$context- A string to identify where the class is being called
- If nothing is passed, the class will use
default
[
'fields' => ['title', 'url', 'image'],
'image_size' => 'large',
'image_type' => 'html',
'acf' => false,
'taxonomies' => false,
];To overwrite the default preset, you just need to pass the keys you want to change:
$args = [
'fields' => ['title'],
];
$getter = new MycGetter($args);
// The arguments used by the class will be:
[
'fields' => ['title'],
'image_size' => 'large',
'image_type' => 'html',
'acf' => false,
'taxonomies' => false,
];If you're using the same arguments in multiples places, you can use the filter myc_getter_presets to create a new preset. Presets can also vary depend on $context:
add_filter('myc_getter_presets', function ($presets, $context) {
$presets['new_preset'] = [
'fields' => ['title'],
'image_size' => 'medium',
];
return $presets;
}, 10, 2);Then, you can use this new preset when instantiate the class:
$getter = new MycGetter('new_preset');If you need to change the image size after creating the getter instance but before generating the image markup, you can use the setImageSize() method:
$getter = new MycGetter('my_preset');
$getter->setImageSize('thumbnail'); // Change from preset's image_size to 'thumbnail'This method supports method chaining:
$getter = new MycGetter('my_preset');
$data = $getter->setImageSize('full')->escapedContent($post);- Format: array
Each field provides a filter to modify the returned content. The filter has three parameters: $value, $post_type and $post_id. Image and URL filters have an additional parameter: $context.
- Returns escaped content of
post->ID - Format: int
- Filter name:
myc_getter_get_id
- Returns escaped content of
post->post_name - Format: string
- Filter name:
myc_getter_get_slug
- Returns escaped content of
post->post_title - Format: string
- Filter name:
myc_getter_get_title
- Returns escaped permalink
- Format: string
- Filter name:
myc_getter_get_url
- Returns image markup or image URL based on the value of
image_typeandimage_size - Format: string
- Filter name:
myc_getter_get_image
- Returns escaped content of
post->post_content - Format: string
- Filter name:
myc_getter_get_text
- Returns escaped content of the function
get_the_excerpt($post) - Format: string
- Filter name:
myc_getter_get_excerpt
- Returns escaped dates of the post in two different format: using the global date format and using ISO 8601 (
c) date format - Format: array
- Filter name:
myc_getter_get_date
- Returns the post type slug
- Format: string
- Filter name:
myc_getter_get_post_type(to avoid redundancy, this filter has only two parameters)
- Format: string
Use one of the default WordPress image sizes (thumbnail, medium, medium_large, large, full) or a custom image size created by add_image_size() function.
- Format: string
Defines the format that the featured image will be returned: html or url.
- Format: array
List of key/value where the key is the field slug and the value is the field type.
Each field type provides a filter to modify the returned content. The filter has four parameters: $value, $post_type, $post_id and $field_slug.
- Returns dates in two different format: using the global date format and using ISO 8601 (
c) date format orfalseif it is empty or the date is not formatted correctly - Format: array
- Filter name:
myc_getter_get_acf_date
- Returns time in global time format or
falseif it is empty or the time is not formatted correctly - Format: string
- Filter name:
myc_getter_get_acf_time
- Returns date and time in global date and time format or
falseif it is empty or the value is not formatted correctly - Format: string
- Filter name:
myc_getter_get_acf_datetime
- Returns escaped url using
esc_url()function - Format: string
- Filter name:
myc_getter_get_acf_url
- Returns escaped link info (
title,url,target,rel) orfalseif it is empty - Format: array
- Filter name:
myc_getter_get_acf_link
- Returns escaped content using
esc_html()function orfalseif it is empty - Format: string
- Filter name:
myc_getter_get_acf_string
- Returns escaped content using
esc_attr()function orfalseif it is empty - Format: string
- Filter name:
myc_getter_get_acf_attr
- Returns escaped content using
wp_kses_post()function orfalseif it is empty - Format: string
- Filter name:
myc_getter_get_acf_text
- Returns the image markup or
falseif the image doesn't exists - This method also works with the Gallery field type
- Pass the image size inside the key using a pipe (e.g.
image_field|medium) - Format: string
- Filter name:
myc_getter_get_acf_image
- Returns the escaped image url or
falseif the image doesn't exists - Pass the image size inside the key using a pipe (e.g.
image_field|medium) - Format: string
- Filter name:
myc_getter_get_acf_image
- Returns the escaped integer value
- Format: int
- Filter name:
myc_getter_get_acf_int
- Returns the escaped email value using the function
antispambot() - Format: string
- Filter name:
myc_getter_get_acf_email
- Returns the content without any treatment
- Format: string
- Filter name:
myc_getter_get_acf_raw
- For repeater fields, pass an array as the value:
'acf' => [
'cards' => ['headline' => 'string', 'button' => 'link', 'text' => 'string']
],- Format: array
List of key/value where the key is the taxonomy slug and the value is the desired returned format.
Each format provides a filter to modify the returned content. The filter has four parameters: $terms, $post_type, $post_id and $taxonomy.
- Returns the term object without any treatment
- Format: object
- Filter name:
myc_getter_get_terms_object
- Returns escaped content of
term->name - Format: string
- Filter name:
myc_getter_get_terms_name
- Returns escaped content of
term->nameandget_term_link()function - Format: array
- Filter name:
myc_getter_get_terms_link
- Returns escaped content of
term->slugandterm->name - Format: array
- Filter name:
myc_getter_get_terms_slug_name
- Returns escaped content of
term->term_idandterm->name - Format: array
- Filter name:
myc_getter_get_terms_id_name