|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Word Count for Post Image Subfields (Caption, Description, Alt, Title) |
| 4 | + * |
| 5 | + * Instruction Video: https://www.loom.com/share/005eda834a6348f9a874a5e6cdd85461 |
| 6 | + * |
| 7 | + * Set min/max word counts for any Post Image subfield (caption, description, alt, title) per field. |
| 8 | + * Supports multiple subfields per field. |
| 9 | + * |
| 10 | + * @version 1.0.0 |
| 11 | + * @author David Smith <[email protected]> |
| 12 | + * @license GPL-2.0+ |
| 13 | + * @link https://gravitywiz.com/ |
| 14 | + */ |
| 15 | +class GW_Post_Image_Word_Count { |
| 16 | + |
| 17 | + private static $_instance = null; |
| 18 | + private $_configs = array(); |
| 19 | + |
| 20 | + public static function get_instance() { |
| 21 | + if ( self::$_instance === null ) { |
| 22 | + self::$_instance = new self(); |
| 23 | + } |
| 24 | + return self::$_instance; |
| 25 | + } |
| 26 | + |
| 27 | + public function __construct( $config = array() ) { |
| 28 | + |
| 29 | + $this->_configs[] = array_merge( array( |
| 30 | + 'form_id' => false, |
| 31 | + 'field_id' => false, |
| 32 | + 'limits' => array() |
| 33 | + ), $config ); |
| 34 | + |
| 35 | + add_action( 'init', array( $this, 'init' ) ); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public function init() { |
| 40 | + |
| 41 | + if ( ! $this->is_applicable() ) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + add_action( 'gform_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 ); |
| 46 | + add_action( 'gform_register_init_scripts', array( $this, 'enqueue_textarea_counter' ), 20 ); |
| 47 | + add_filter( 'gform_validation', array( $this, 'validate_word_count' ) ); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public function is_applicable() { |
| 52 | + return ! empty( $this->_configs ); |
| 53 | + } |
| 54 | + |
| 55 | + // Map subfield keys to their input suffixes and labels. |
| 56 | + public function get_subfields() { |
| 57 | + return array( |
| 58 | + 'caption' => array( 'suffix' => '4', 'label' => __( 'Caption', 'gravityforms' ) ), |
| 59 | + 'description' => array( 'suffix' => '7', 'label' => __( 'Description', 'gravityforms' ) ), |
| 60 | + 'alt' => array( 'suffix' => '2', 'label' => __( 'Alt Text', 'gravityforms' ) ), |
| 61 | + 'title' => array( 'suffix' => '1', 'label' => __( 'Title', 'gravityforms' ) ), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function enqueue_scripts( $form ) { |
| 66 | + // Check if any Post Image field is configured for word count. |
| 67 | + foreach ( $this->_configs as $config ) { |
| 68 | + if ( ( ! $config['form_id'] || $form['id'] == $config['form_id'] ) && $config['field_id'] ) { |
| 69 | + foreach ( $form['fields'] as $field ) { |
| 70 | + if ( $field->id == $config['field_id'] && $field->type === 'post_image' ) { |
| 71 | + // Force enqueue the GP Word Count JS. |
| 72 | + wp_enqueue_script( 'gp-word-count', plugins_url( 'scripts/jquery.textareaCounter.js', 'gwwordcount/gwwordcount.php' ), array( 'jquery' ), null, true ); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function enqueue_textarea_counter( $form ) { |
| 81 | + |
| 82 | + foreach ( $this->_configs as $config ) { |
| 83 | + if ( ! $this->is_config_applicable_to_form( $config, $form ) ) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + $field = GFAPI::get_field( $form, $config['field_id'] ); |
| 88 | + if ( ! $field || $field->type !== 'post_image' || empty( $config['limits'] ) ) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $form_id = $form['id']; |
| 93 | + $field_id = $field->id; |
| 94 | + |
| 95 | + foreach ( $config['limits'] as $subfield => $limits ) { |
| 96 | + $subfields = $this->get_subfields(); |
| 97 | + if ( empty( $subfields[ $subfield ] ) ) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $suffix = $subfields[ $subfield ]['suffix']; |
| 102 | + $min = isset( $limits['min'] ) ? intval( $limits['min'] ) : 0; |
| 103 | + $max = isset( $limits['max'] ) ? intval( $limits['max'] ) : 0; |
| 104 | + |
| 105 | + $args = array( |
| 106 | + 'formId' => $form_id, |
| 107 | + 'limit' => $max, |
| 108 | + 'min' => $min, |
| 109 | + 'truncate' => true, |
| 110 | + 'defaultLabel' => sprintf( __( 'Max: %s words', 'gp-word-count' ), '{limit}' ), |
| 111 | + 'defaultLabelSingular' => sprintf( __( 'Max: %s word', 'gp-word-count' ), '{limit}' ), |
| 112 | + 'counterLabel' => sprintf( __( '%s words left', 'gp-word-count' ), '{remaining}' ), |
| 113 | + 'counterLabelSingular' => sprintf( __( '%s word left', 'gp-word-count' ), '{remaining}' ), |
| 114 | + 'limitReachedLabel' => '<span class="gwwc-max-reached" style="font-weight:bold;">' . sprintf( __( '%s words left', 'gp-word-count' ), '{remaining}' ) . '</span>', |
| 115 | + 'limitExceededLabel' => '<span class="gwwc-max-exceeded" style="font-weight:bold;color:#c0392b;">' . sprintf( __( 'Limit exceeded!', 'gp-word-count' ), '{remaining}' ) . '</span>', |
| 116 | + 'minCounterLabel' => sprintf( __( '%s more words required', 'gp-word-count' ), '{remaining}' ), |
| 117 | + 'minCounterLabelSingular' => sprintf( __( '%s more word required', 'gp-word-count' ), '{remaining}' ), |
| 118 | + 'minReachedLabel' => '<span class="gwwc-min-reached" style="font-weight:bold;color:#27ae60">' . __( 'Minimum word count met.', 'gp-word-count' ) . '</span>', |
| 119 | + 'minDefaultLabel' => sprintf( __( 'Min: %s words', 'gp-word-count' ), '{min}' ), |
| 120 | + 'minDefaultLabelSingular' => sprintf( __( 'Min: %s word', 'gp-word-count' ), '{min}' ), |
| 121 | + ); |
| 122 | + |
| 123 | + $args_json = json_encode( $args ); |
| 124 | + $input_id = "input_{$form_id}_{$field_id}_{$suffix}"; |
| 125 | + $script = "jQuery('#{$input_id}').textareaCounter({$args_json});"; |
| 126 | + |
| 127 | + GFFormDisplay::add_init_script( $form_id, "gwwc_post_image_{$subfield}_wc_{$field_id}", GFFormDisplay::ON_PAGE_RENDER, $script ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + public function validate_word_count( $result ) { |
| 134 | + |
| 135 | + $form = $result['form']; |
| 136 | + |
| 137 | + foreach ( $this->_configs as $config ) { |
| 138 | + if ( ! $this->is_config_applicable_to_form( $config, $form ) ) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + foreach ( $form['fields'] as &$field ) { |
| 143 | + if ( $field->id != $config['field_id'] || $field->type !== 'post_image' || empty( $config['limits'] ) ) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + foreach ( $config['limits'] as $subfield => $limits ) { |
| 148 | + $subfields = $this->get_subfields(); |
| 149 | + if ( empty( $subfields[ $subfield ] ) ) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + $suffix = $subfields[ $subfield ]['suffix']; |
| 154 | + $label = $subfields[ $subfield ]['label']; |
| 155 | + $input_name = "input_{$field->id}_{$suffix}"; |
| 156 | + $value = rgpost( $input_name ); |
| 157 | + $word_count = preg_match_all( '/\S+/', trim( $value ) ); |
| 158 | + |
| 159 | + $min = isset( $limits['min'] ) ? intval( $limits['min'] ) : 0; |
| 160 | + $max = isset( $limits['max'] ) ? intval( $limits['max'] ) : 0; |
| 161 | + |
| 162 | + if ( $min && $word_count < $min ) { |
| 163 | + $field->failed_validation = true; |
| 164 | + $field->validation_message = sprintf( |
| 165 | + _n( '%s must be at least %s word.', '%s must be at least %s words.', $min, 'gp-word-count' ), |
| 166 | + $label, $min |
| 167 | + ); |
| 168 | + $result['is_valid'] = false; |
| 169 | + } |
| 170 | + |
| 171 | + if ( $max && $word_count > $max ) { |
| 172 | + $field->failed_validation = true; |
| 173 | + $field->validation_message = sprintf( |
| 174 | + _n( '%s may only be %s word.', '%s may only be %s words.', $max, 'gp-word-count' ), |
| 175 | + $label, $max |
| 176 | + ); |
| 177 | + $result['is_valid'] = false; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + $result['form'] = $form; |
| 184 | + return $result; |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + private function is_config_applicable_to_form( $config, $form ) { |
| 189 | + return ( ! $config['form_id'] || $form['id'] == $config['form_id'] ) && $config['field_id']; |
| 190 | + } |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +// CONFIGURATION: Set your field IDs and subfield limits here. |
| 195 | +new GW_Post_Image_Word_Count( array( |
| 196 | + 'form_id' => 1, // Form ID (optional, can be false to apply to all forms) |
| 197 | + 'field_id' => 3, // Post Image field ID |
| 198 | + 'limits' => array( |
| 199 | + 'caption' => array( 'min' => 3, 'max' => 10 ), |
| 200 | + 'description' => array( 'min' => 2, 'max' => 20 ), |
| 201 | + // 'alt' => array( 'min' => 1, 'max' => 5 ), |
| 202 | + // 'title' => array( 'min' => 1, 'max' => 5 ), |
| 203 | + ), |
| 204 | +) ); |
| 205 | + |
| 206 | +// Add more configurations as needed... |
| 207 | +// new GW_Post_Image_Word_Count( array( |
| 208 | +// 'form_id' => 2, |
| 209 | +// 'field_id' => 8, |
| 210 | +// 'limits' => array( |
| 211 | +// 'caption' => array( 'min' => 5, 'max' => 15 ), |
| 212 | +// ), |
| 213 | +// ) ); |
0 commit comments