Advertisement
geminilabs

[site-reviews] Add custom form fields

Oct 10th, 2018 (edited)
4,738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2. //
  3. // ADDING CUSTOM FIELDS TO THE REVIEW FORM IS NOT ACTIVELY SUPPORTED!
  4. // However, if you would like to try and do this on your own, this should help get you started.
  5. // This will only work with Site Reviews 6/7
  6. //
  7. // Alternatively, please see: https://niftyplugins.com/plugins/site-reviews-forms/
  8. //
  9.  
  10. /**
  11.  * Modify the review form fields here
  12.  * @param array $config
  13.  * @return array
  14.  */
  15. add_filter('site-reviews/config/forms/review-form', function ($config) {
  16.     $config['opinion'] = [
  17.         'label' => __('Provide an opinion', 'your_theme_domain'),
  18.         'required' => true,
  19.         'type' => 'textarea',
  20.     ];
  21.     $config['service_rating'] = [
  22.         'label' => __('Select a rating for the service', 'your_theme_domain'),
  23.         'required' => true,
  24.         'type' => 'rating',
  25.     ];
  26.     return $config;
  27. });
  28.  
  29. /**
  30.  * Perform custom field validation here
  31.  * @param bool $isValid
  32.  * @param array $requestData
  33.  * @return bool|string
  34.  */
  35. add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
  36.     // return true on success
  37.     // or return false on failure
  38.     // or return a custom error message string on failure
  39.     return $isValid;
  40. }, 10, 2);
  41.  
  42. /**
  43.  * Triggered immediately after a review is saved
  44.  * If you are uploading files, this is where you would save them to the database and attach them to the review
  45.  * @param \GeminiLabs\SiteReviews\Review $review
  46.  * @param \GeminiLabs\SiteReviews\Commands\CreateReview $command
  47.  * @return void
  48.  */
  49. add_action('site-reviews/review/created', function ($review, $command) {
  50.     // You may access the global $_POST and $_FILES here.
  51. }, 10, 2);
  52.  
  53. /**
  54.  * Set the default values of the custom fields here
  55.  * @param \GeminiLabs\SiteReviews\Review $review
  56.  * @return \GeminiLabs\SiteReviews\Review
  57.  */
  58. add_action('site-reviews/review/build/before', function ($review) {
  59.     if (!isset($review->custom->opinion)) {
  60.         $review->custom->set('opinion', '');
  61.     }
  62.     if (!isset($review->custom->service_rating)) {
  63.         $review->custom->set('service_rating', 0);
  64.     }
  65. });
  66.  
  67. /**
  68.  * Render the custom review fields here
  69.  * In order to display the rendered custom fields, you will need to use a custom "review.php" template
  70.  * as shown in the plugin FAQ ("How do I change the order of the review fields?")
  71.  * and you will need to add your custom template tags to it, for example: {{ name_of_your_custom_field }}
  72.  * @param array $templateTags
  73.  * @param \GeminiLabs\SiteReviews\Review $review
  74.  * @return array
  75.  */
  76. add_filter('site-reviews/review/build/after', function ($templateTags, $review) {
  77.     foreach ($review->custom as $key => $value) {
  78.         if ('service_rating' === $key) {
  79.             $value = glsr_star_rating($value); // render the stars from the rating value
  80.         }
  81.         if (is_array($value)) {
  82.             $list = array_reduce($value, function ($result, $item) {
  83.                 return "{$result}<li>{$item}</li>";
  84.             });
  85.             $value = "<ul>{$list}</ul>";
  86.         }
  87.         $templateTags[$key] = "<div class=\"glsr-custom-{$key}\">{$value}</div>";
  88.     }
  89.     return $templateTags;
  90. }, 10, 2);
  91.  
  92. /**
  93.  * Enable the Custom Fields metabox to display the values of the submitted custom fields
  94.  * @return void
  95.  */
  96. add_action( 'admin_init', function() {
  97.     add_post_type_support('site-review', 'custom-fields');
  98. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement