Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // wade site Reviews
- /**
- * 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['rating_one'] = [
- 'label' => __('Rating One', 'your_theme_domain'),
- 'required' => true,
- 'type' => 'rating',
- ];
- $config['rating_two'] = [
- 'label' => __('Rating Two', 'your_theme_domain'),
- 'required' => true,
- 'type' => 'rating',
- ];
- $config['rating_three'] = [
- 'label' => __('Rating Three', 'your_theme_domain'),
- 'required' => true,
- 'type' => 'rating',
- ];
- $config['rating_four'] = [
- 'label' => __('Rating Four', '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',
- 'rating_one', // this is a custom field key
- 'rating_two', // this is a custom field key
- 'rating_three', // this is a custom field key
- 'rating_four', // this is a custom field key
- 'title',
- 'content',
- '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['rating_one'] = 'required';
- $rules['rating_two'] = 'required';
- $rules['rating_three'] = 'required';
- $rules['rating_four'] = 'required';
- return $rules;
- });
- /**
- * Perform custom validation here if needed
- * Paste this in your active theme's functions.php file.
- * @param bool $isValid
- * @param array $requestData
- * @return bool|string
- */
- add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
- // return true on success
- // or return false on failure
- // or return a custom error message string on failure
- return $isValid;
- }, 10, 2);
- /**
- * 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 = ['rating_one', 'rating_two', 'rating_three', 'rating_four']; // 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, [
- 'rating_one' => 0,
- 'rating_two' => 0,
- 'rating_three' => 0,
- 'rating_four' => 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 template tags to it, for example: {{ name_of_your_custom_field }}
- * @param array $templateTags
- * @param \GeminiLabs\SiteReviews\Review $review
- * @return array
- */
- add_filter('site-reviews/review/build/after', function ($templateTags, $review) {
- $ratingKeys = ['rating_one', 'rating_two', 'rating_three', 'rating_four']; // 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>';
- }
- $templateTags[$key] = '<div class="glsr-custom-'.$key.'">'.$value.'</div>';
- }
- return $templateTags;
- }, 10, 2);
- /**
- * Modifies the CSS classes used for the form fields
- * @param array $classes
- * @param array $field
- * @return array
- */
- add_filter('site-reviews/rendered/field/classes', function ($classes, $fieldValues) {
- $fieldName = glsr_get($fieldValues, 'path');
- $fieldNames = [
- 'rating_one',
- 'rating_two',
- 'rating_three',
- 'rating_four',
- ];
- if (in_array($fieldName, $fieldNames)) {
- $classes[] = 'column-class'; // add your class here as needed.
- }
- return $classes;
- }, 10, 2);
- /**
- * Adds a link of the assigned page to the Site Reviews summary text
- * Paste this in your active theme's functions.php file.
- * @param array $fields
- * @return array
- */
- add_filter( 'site-reviews/rendered/template/reviews-summary', function( $template, $data ) {
- if( !empty( $data['context']['assigned_to'] )) {
- $permalink = get_the_permalink( $data['context']['assigned_to'] );
- $title = get_the_title( $data['context']['assigned_to'] );
- if( $permalink && $title ) {
- $assignedToPermalink = sprintf( ' of <a href="%s">%s</a>', $permalink, $title );
- $template = preg_replace( '/(\(based on \d+ reviews?)(\))/', '$1'.$assignedToPermalink.'$2', $template );
- }
- }
- return $template;
- }, 10, 2 );
- // wade site Reviews end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement