geminilabs

[site-reviews] Add custom form fields

Oct 10th, 2018 (edited)
3,273
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //
  3. // ADDING CUSTOM FIELDS TO THE SUBMISSION 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 5
  6. //
  7. // Alternatively, please see: https://niftyplugins.com/plugins/site-reviews-forms/
  8. //
  9.  
  10. /**
  11.  * Modifies the submission form fields
  12.  * Paste this in your active theme's functions.php file.
  13.  * @param array $config
  14.  * @return array
  15.  */
  16. add_filter('site-reviews/config/forms/review-form', function ($config) {
  17.     $config['colours'] = [
  18.         'label' => __('Select your favourite colours', 'your_theme_domain'),
  19.         'options' => [
  20.             'Blue' => __('Blue', 'your_theme_domain'),
  21.             'Red' => __('Red', 'your_theme_domain'),
  22.             'Yellow' => __('Yellow', 'your_theme_domain'),
  23.         ],
  24.         'type' => 'checkbox',
  25.     ];
  26.     $config['opinion'] = [
  27.         'label' => __('Opinion', 'your_theme_domain'),
  28.         'placeholder' => __('Provide an opinion', 'your_theme_domain'),
  29.         'required' => true,
  30.         'type' => 'textarea',
  31.     ];
  32.     $config['service_rating'] = [
  33.         'label' => __('Select a rating for the service', 'your_theme_domain'),
  34.         'required' => true,
  35.         'type' => 'rating',
  36.     ];
  37.     return $config;
  38. });
  39.  
  40. /**
  41.  * Perform custom validation here if needed
  42.  * Paste this in your active theme's functions.php file.
  43.  * @param bool $isValid
  44.  * @param array $requestData
  45.  * @return bool|string
  46.  */
  47. add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
  48.     // return true on success
  49.     // or return false on failure
  50.     // or return a custom error message string on failure
  51.     return $isValid;
  52. }, 10, 2);
  53.  
  54. /**
  55.  * Triggered immediately after a review is saved
  56.  * If you are uploading files, this is where you would save them to the database and attach them to the review
  57.  * Paste this in your active theme's functions.php file.
  58.  * @param \GeminiLabs\SiteReviews\Review $review
  59.  * @param \GeminiLabs\SiteReviews\Commands\CreateReview $command
  60.  * @return void
  61.  */
  62. add_action('site-reviews/review/created', function ($review, $command) {
  63.     // You may access the global $_POST and $_FILES here.
  64. }, 10, 2);
  65.  
  66. /**
  67.  * Set the default values of the custom fields here
  68.  * Paste this in your active theme's functions.php file.
  69.  * @param \GeminiLabs\SiteReviews\Review $review
  70.  * @return \GeminiLabs\SiteReviews\Review
  71.  */
  72. add_filter('site-reviews/review/build/before', function ($review) {
  73.     $review->custom = wp_parse_args($review->custom, [
  74.         'colours' => '',
  75.         'opinion' => '',
  76.         'service_rating' => 0,
  77.     ]);
  78.     return $review;
  79. });
  80.  
  81. /**
  82.  * Renders the custom review fields
  83.  * Paste this in your active theme's functions.php file.
  84.  * In order to display the rendered custom fields, you will need to use a custom "review.php" template
  85.  * as shown in the plugin FAQ ("How do I change the order of the review fields?")
  86.  * and you will need to add your custom template tags to it, for example: {{ name_of_your_custom_field }}
  87.  * @param array $templateTags
  88.  * @param \GeminiLabs\SiteReviews\Review $review
  89.  * @return array
  90.  */
  91. add_filter('site-reviews/review/build/after', function ($templateTags, $review) {
  92.     foreach ($review->custom as $key => $value) {
  93.         if ('service_rating' === $key) {
  94.             $value = glsr_star_rating($value); // render the stars from the rating value
  95.         }
  96.         if (is_array($value)) {
  97.             $list = array_reduce($value, function ($result, $item) {
  98.                 return $result.'<li>'.$item.'</li>';
  99.             });
  100.             $value = '<ul>'.$list.'</ul>';
  101.         }
  102.         $templateTags[$key] = '<div class="glsr-custom-'.$key.'">'.$value.'</div>';
  103.     }
  104.     return $templateTags;
  105. }, 10, 2);
  106.  
  107. /**
  108.  * Enables the Custom Fields metabox to display the values of the submitted custom fields
  109.  * Paste this in your active theme's functions.php file.
  110.  * @return void
  111.  */
  112. add_action( 'admin_init', function() {
  113.     add_post_type_support('site-review', 'custom-fields');
  114. });
RAW Paste Data