Advertisement
wadereynes

site-reviews

Sep 24th, 2020
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.67 KB | None | 0 0
  1. // wade site Reviews
  2. /**
  3.  * Modifies the submission form fields
  4.  * Paste this in your active theme's functions.php file.
  5.  * @param array $config
  6.  * @return array
  7.  */
  8. add_filter('site-reviews/config/forms/submission-form', function ($config) {
  9.  
  10.     $config['rating_one'] = [
  11.         'label' => __('Rating One', 'your_theme_domain'),
  12.         'required' => true,
  13.         'type' => 'rating',
  14.     ];
  15.     $config['rating_two'] = [
  16.         'label' => __('Rating Two', 'your_theme_domain'),
  17.         'required' => true,
  18.         'type' => 'rating',
  19.     ];
  20.     $config['rating_three'] = [
  21.         'label' => __('Rating Three', 'your_theme_domain'),
  22.         'required' => true,
  23.         'type' => 'rating',
  24.     ];
  25.         $config['rating_four'] = [
  26.                 'label' => __('Rating Four', 'your_theme_domain'),
  27.                 'required' => true,
  28.                 'type' => 'rating',
  29.         ];
  30.     return $config;
  31. });
  32.  
  33. /**
  34.  * Customises the order of the fields used in the Site Reviews submission form.
  35.  * @param array $order
  36.  * @return array
  37.  */
  38. add_filter('site-reviews/submission-form/order', function ($order) {
  39.     // The $order array contains the field keys returned below.
  40.     // Simply add any custom field keys that you have added and change them to the desired order.
  41.     return [
  42.         'rating',
  43.         'rating_one', // this is a custom field key
  44.         'rating_two', // this is a custom field key
  45.         'rating_three', // this is a custom field key
  46.         'rating_four', // this is a custom field key
  47.         'title',
  48.         'content',
  49.         'name',
  50.         'email',
  51.         'terms',
  52.     ];
  53. });
  54.  
  55. /**
  56.  * Modifies the submission form field rules
  57.  * Paste this in your active theme's functions.php file.
  58.  * @param array $rules
  59.  * @return array
  60.  */
  61. add_filter('site-reviews/validation/rules', function ($rules) {
  62.     $rules['rating_one'] = 'required';
  63.     $rules['rating_two'] = 'required';
  64.     $rules['rating_three'] = 'required';
  65.     $rules['rating_four'] = 'required';
  66.     return $rules;
  67. });
  68.  
  69. /**
  70.  * Perform custom validation here if needed
  71.  * Paste this in your active theme's functions.php file.
  72.  * @param bool $isValid
  73.  * @param array $requestData
  74.  * @return bool|string
  75.  */
  76. add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
  77.     // return true on success
  78.     // or return false on failure
  79.     // or return a custom error message string on failure
  80.     return $isValid;
  81. }, 10, 2);
  82.  
  83. /**
  84.  * Triggered immediately after a review is saved
  85.  * If you are uploading files, this is where you would save them to the database and attach them to the review
  86.  * Paste this in your active theme's functions.php file.
  87.  * @param \GeminiLabs\SiteReviews\Review $review
  88.  * @param \GeminiLabs\SiteReviews\Commands\CreateReview $command
  89.  * @return void
  90.  */
  91. add_action('site-reviews/review/created', function ($review, $command) {
  92.     // You may access the global $_POST and $_FILES here.
  93. }, 10, 2);
  94.  
  95. /**
  96.  * Displays custom fields in the Review's "Details" metabox
  97.  * Paste this in your active theme's functions.php file.
  98.  * @param array $metabox
  99.  * @param \GeminiLabs\SiteReviews\Review $review
  100.  * @return array
  101.  */
  102. add_filter('site-reviews/metabox/details', function ($metabox, $review) {
  103.     $ratingKeys = ['rating_one', 'rating_two', 'rating_three', 'rating_four']; // change these as needed
  104.     foreach ($review->custom as $key => $value) {
  105.         if (in_array($key, $ratingKeys)) {
  106.             $value = glsr_star_rating($value); // render the stars from the rating value
  107.         }
  108.         if (is_array($value)) {
  109.             $value = implode(', ', $value);
  110.         }
  111.         $metabox[$key] = $value;
  112.     }
  113.     return $metabox;
  114. }, 10, 2);
  115.  
  116. /**
  117.  * Set the default values of the custom fields here
  118.  * Paste this in your active theme's functions.php file.
  119.  * @param \GeminiLabs\SiteReviews\Review $review
  120.  * @return \GeminiLabs\SiteReviews\Review
  121.  */
  122. add_filter('site-reviews/review/build/before', function ($review) {
  123.     $review->custom = wp_parse_args($review->custom, [
  124.         'rating_one' => 0,
  125.         'rating_two' => 0,
  126.         'rating_three' => 0,
  127.         'rating_four' => 0,
  128.     ]);
  129.     return $review;
  130. });
  131.  
  132. /**
  133.  * Renders the custom review fields
  134.  * Paste this in your active theme's functions.php file.
  135.  * In order to display the rendered custom fields, you will need to use a custom "review.php" template
  136.  * as shown in the plugin FAQ ("How do I change the order of the review fields?")
  137.  * and you will need to add your custom template tags to it, for example: {{ name_of_your_custom_field }}
  138.  * @param array $templateTags
  139.  * @param \GeminiLabs\SiteReviews\Review $review
  140.  * @return array
  141.  */
  142. add_filter('site-reviews/review/build/after', function ($templateTags, $review) {
  143.     $ratingKeys = ['rating_one', 'rating_two', 'rating_three', 'rating_four']; // change these as needed
  144.     foreach ($review->custom as $key => $value) {
  145.         if (in_array($key, $ratingKeys)) {
  146.             $value = glsr_star_rating($value); // render the stars from the rating value
  147.         }
  148.         if (is_array($value)) {
  149.             $list = array_reduce($value, function ($result, $item) {
  150.                 return $result.'<li>'.$item.'</li>';
  151.             });
  152.             $value = '<ul>'.$list.'</ul>';
  153.         }
  154.         $templateTags[$key] = '<div class="glsr-custom-'.$key.'">'.$value.'</div>';
  155.     }
  156.     return $templateTags;
  157. }, 10, 2);
  158.  
  159. /**
  160.  * Modifies the CSS classes used for the form fields
  161.  * @param array $classes
  162.  * @param array $field
  163.  * @return array
  164.  */
  165. add_filter('site-reviews/rendered/field/classes', function ($classes, $fieldValues) {
  166.     $fieldName = glsr_get($fieldValues, 'path');
  167.     $fieldNames = [
  168.         'rating_one',
  169.         'rating_two',
  170.         'rating_three',
  171.         'rating_four',
  172.     ];
  173.     if (in_array($fieldName, $fieldNames)) {
  174.         $classes[] = 'column-class'; // add your class here as needed.
  175.     }
  176.     return $classes;
  177. }, 10, 2);
  178.  
  179. /**
  180.  * Adds a link of the assigned page to the Site Reviews summary text
  181.  * Paste this in your active theme's functions.php file.
  182.  * @param array $fields
  183.  * @return array
  184.  */
  185. add_filter( 'site-reviews/rendered/template/reviews-summary', function( $template, $data ) {
  186.     if( !empty( $data['context']['assigned_to'] )) {
  187.         $permalink = get_the_permalink( $data['context']['assigned_to'] );
  188.         $title = get_the_title( $data['context']['assigned_to'] );
  189.         if( $permalink && $title ) {
  190.             $assignedToPermalink = sprintf( ' of <a href="%s">%s</a>', $permalink, $title );
  191.             $template = preg_replace( '/(\(based on \d+ reviews?)(\))/', '$1'.$assignedToPermalink.'$2', $template );
  192.         }
  193.     }
  194.     return $template;
  195. }, 10, 2 );
  196. // wade site Reviews end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement