Skip to content

Instantly share code, notes, and snippets.

@axxe16
Created May 8, 2020 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axxe16/1f985aa25ec1861422ee2e4ae25da6c4 to your computer and use it in GitHub Desktop.
Save axxe16/1f985aa25ec1861422ee2e4ae25da6c4 to your computer and use it in GitHub Desktop.
customizzazione site reviews #plugin #wp #site-review #custom
//RIF: https://wordpress.org/plugins/site-reviews/
//
// ADDING CUSTOM FIELDS TO THE SUBMISSION FORM IS NOT ACTIVELY SUPPORTED!
// However, if you would like to try and do this on your own, this should help get you started.
//
/**
* Modifies the submission form fields
* Paste this in your active theme's functions.php file.
* @param array $config
* @return array
*/
add_filter('site-reviews/config/forms/submission-form', function ($config) {
$config['colours'] = [
'label' => __('Select your favourite colours', 'your_theme_domain'),
'options' => [
'Blue' => __('Blue', 'your_theme_domain'),
'Red' => __('Red', 'your_theme_domain'),
'Yellow' => __('Yellow', 'your_theme_domain'),
],
'type' => 'checkbox',
];
$config['opinion'] = [
'label' => __('Opinion', 'your_theme_domain'),
'placeholder' => __('Provide an opinion', 'your_theme_domain'),
'required' => true,
'type' => 'textarea',
];
$config['personnel_rating'] = [
'label' => __('Select a rating for the personnel', 'your_theme_domain'),
'required' => true,
'type' => 'rating',
];
$config['service_rating'] = [
'label' => __('Select a rating for the service', 'your_theme_domain'),
'required' => true,
'type' => 'rating',
];
return $config;
});
/**
* Customises the order of the fields used in the Site Reviews submission form.
* @param array $order
* @return array
*/
add_filter('site-reviews/submission-form/order', function ($order) {
// The $order array contains the field keys returned below.
// Simply add any custom field keys that you have added and change them to the desired order.
return [
'rating',
'personnel_rating', // this is a custom field key
'service_rating', // this is a custom field key
'title',
'content',
'colours', // this is a custom field key
'opinion', // this is a custom field key
'name',
'email',
'terms',
];
});
/**
* Modifies the submission form field rules
* Paste this in your active theme's functions.php file.
* @param array $rules
* @return array
*/
add_filter('site-reviews/validation/rules', function ($rules) {
$rules['opinion'] = 'required|max:100'; // maximum of 100 characters
$rules['personnel_rating'] = 'required';
$rules['service_rating'] = 'required';
return $rules;
});
/**
* Triggered immediately after a review is saved
* If you are uploading files, this is where you would save them to the database and attach them to the review
* Paste this in your active theme's functions.php file.
* @param \GeminiLabs\SiteReviews\Review $review
* @param \GeminiLabs\SiteReviews\Commands\CreateReview $command
* @return void
*/
add_action('site-reviews/review/created', function ($review, $command) {
// You may access the global $_POST and $_FILES here.
}, 10, 2);
/**
* Displays custom fields in the Review's "Details" metabox
* Paste this in your active theme's functions.php file.
* @param array $metabox
* @param \GeminiLabs\SiteReviews\Review $review
* @return array
*/
add_filter('site-reviews/metabox/details', function ($metabox, $review) {
$ratingKeys = ['personnel_rating', 'service_rating']; // change these as needed
foreach ($review->custom as $key => $value) {
if (in_array($key, $ratingKeys)) {
$value = glsr_star_rating($value); // render the stars from the rating value
}
if (is_array($value)) {
$value = implode(', ', $value);
}
$metabox[$key] = $value;
}
return $metabox;
}, 10, 2);
/**
* Set the default values of the custom fields here
* Paste this in your active theme's functions.php file.
* @param \GeminiLabs\SiteReviews\Review $review
* @return \GeminiLabs\SiteReviews\Review
*/
add_filter('site-reviews/review/build/before', function ($review) {
$review->custom = wp_parse_args($review->custom, [
'colours' => '',
'opinion' => '',
'personnel_rating' => 0,
'service_rating' => 0,
]);
return $review;
});
/**
* Renders the custom review fields
* Paste this in your active theme's functions.php file.
* In order to display the rendered custom fields, you will need to use a custom "review.php" template
* as shown in the plugin FAQ ("How do I change the order of the review fields?")
* and you will need to add your custom keys to it, for example: {{ name_of_your_custom_key }}
* @param array $renderedFields
* @param \GeminiLabs\SiteReviews\Review $review
* @return array
*/
add_filter('site-reviews/review/build/after', function ($renderedFields, $review) {
$ratingKeys = ['personnel_rating', 'service_rating']; // change these as needed
foreach ($review->custom as $key => $value) {
if (in_array($key, $ratingKeys)) {
$value = glsr_star_rating($value); // render the stars from the rating value
}
if (is_array($value)) {
$list = array_reduce($value, function ($result, $item) {
return $result.'<li>'.$item.'</li>';
});
$value = '<ul>'.$list.'</ul>';
}
$renderedFields[$key] = '<div class="glsr-custom-'.$key.'">'.$value.'</div>';
}
return $renderedFields;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment