diff --git a/includes/fields/class-gravityview-field-is-read.php b/includes/fields/class-gravityview-field-is-read.php new file mode 100644 index 000000000..c2ce8d464 --- /dev/null +++ b/includes/fields/class-gravityview-field-is-read.php @@ -0,0 +1,236 @@ +label = esc_html__( 'Read Status', 'gk-gravityview' ); + $this->default_search_label = __( 'Is Read', 'gk-gravityview' ); + $this->description = esc_html__( 'Display whether the entry has been read.', 'gk-gravityview' ); + + $this->add_hooks(); + + parent::__construct(); + } + + /** + * Prevents overriding Gravity Forms entry meta, even though it's a meta field. + * + * @since TBD + * + * @param array $entry_meta Existing entry meta. + * + * @return array + */ + public function add_entry_meta( $entry_meta ) { + return $entry_meta; + } + + /** + * Adds field hooks. + * + * @since TBD + */ + private function add_hooks() { + /** @see Field::get_value_filters */ + add_filter( 'gravityview/field/is_read/value', [ $this, 'get_value' ], 10, 3 ); + add_action( 'gravityview/template/after', [ $this, 'print_script' ], 10, 1 ); + } + + /** + * {@inheritDoc} + * + * @since TBD + */ + public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { + $field_options['is_read_label'] = [ + 'type' => 'text', + 'label' => __( 'Read Label', 'gk-gravityview' ), + 'desc' => __( 'If the entry has been read, display this value', 'gk-gravityview' ), + 'value' => __( 'Read', 'gk-gravityview' ), + ]; + + $field_options['is_unread_label'] = [ + 'type' => 'text', + 'label' => __( 'Unread Label', 'gk-gravityview' ), + 'desc' => __( 'If the entry has not been read, display this value', 'gk-gravityview' ), + 'value' => __( 'Unread', 'gk-gravityview' ), + ]; + + return $field_options; + } + + /** + * Displays the value based on the field settings. + * + * @since 2.0 + * + * @param string $value The value. + * @param Field $field The field we're doing this for. + * @param View $view The view for this context if applicable. + * + * @return string Value of the field + */ + public function get_value( $value, $field, $view ) { + if ( empty( $value ) ) { + return Utils::get( $field, 'is_unread_label', esc_html__( 'Unread', 'gk-gravityview' ) ); + } + + return $this->get_is_read_label( $field, $view ); + } + + /** + * Returns the field's "Read" label. + * + * @since TBD + * + * @param Field $field The field. + * @param View $view The View. + * + * @return string The string to use for "Read". + */ + protected function get_is_read_label( $field, $view ) { + $label = Utils::get( $field, 'is_read_label', esc_html__( 'Read', 'gk-gravityview' ) ); + + /** + * Modify the "Read" label. + * + * @filter `gk/gravityview/field/is-read/read-label` + * + * @since TBD + * + * @param string $label The label. + * @param Field $field The field. + * @param View $view The View. + */ + $label = apply_filters( 'gk/gravityview/field/is-read/read-label', $label, $field, $view ); + + return $label; + } + + /** + * Returns the first "Read Status" field from the context. + * + * @since TBD + * + * @param Template_Context $context The context. + * + * @return Field|null The field or null if not found. + */ + protected function get_field_from_context( $context ) { + foreach ( $context->fields->all() as $field ) { + if ( $this->name === $field->type ) { + return $field; + } + } + + return null; + } + + /** + * Adds JS to the bottom of the View if there is a read field and user has `gravityview_edit_entries` capability. + * + * @since 2.0 + * + * @param Template_Context $context The template context. + * + * @return void + */ + public function print_script( $context ) { + if ( ! GravityView_Roles_Capabilities::has_cap( 'gravityview_edit_entries' ) ) { + return; + } + + /** + * Disable the script that marks the entry as read. + * + * @filter `gk/gravityview/field/is-read/print-script` + * + * @since TBD + * + * @param bool $print_script Whether the script be printed? Default: true. + * @param Template_Context $context The template context. + */ + if ( ! apply_filters( 'gk/gravityview/field/is-read/print-script', true, $context ) ) { + return; + } + + $entry = gravityview()->request->is_entry(); + + if ( empty( $entry['is_read'] ) ) { + return; + } + + $field = $this->get_field_from_context( $context ); + $read_label = $this->get_is_read_label( $field, $context->view ); + ?> + + widget_id = 'search_bar'; + $this->widget_description = esc_html__( 'Display a search form for users to search a View\'s entries.', 'gk-gravityview' ); $this->widget_subtitle = esc_html__( 'Search form for searching entries.', 'gk-gravityview' ); @@ -401,6 +402,20 @@ public static function render_searchable_fields( $form_id = null, $current = '' 'text' => esc_html__( 'Is Starred', 'gk-gravityview' ), 'type' => 'boolean', ), + 'is_read' => array( + 'text' => esc_html__( 'Is Read', 'gravityview' ), + 'type' => 'select', + 'choices' => array( + array( + 'text' => __( 'Read', 'gravityview' ), + 'value' => '1', + ), + array( + 'text' => __( 'Unread', 'gravityview' ), + 'value' => '0', + ), + ), + ), ); if ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) { @@ -1692,6 +1707,21 @@ public function render_frontend( $widget_args, $content = '', $context = '' ) { $updated_field['value'] = $this->rgget_or_rgpost( 'filter_is_approved' ); $updated_field['choices'] = self::get_is_approved_choices(); break; + + case 'is_read': + $updated_field['key'] = 'is_read'; + $updated_field['value'] = $this->rgget_or_rgpost( 'filter_is_read' ); + $updated_field['choices'] = array( + array( + 'text' => __( 'Unread', 'gravityview' ), + 'value' => 0, + ), + array( + 'text' => __( 'Read', 'gravityview' ), + 'value' => 1, + ), + ); + break; } $search_fields[ $k ] = $updated_field; @@ -1908,6 +1938,11 @@ private function get_search_filter_details( $field, $context, $widget_args ) { $filter['type'] = 'created_by'; } + if( 'payment_status' === $field['field'] ) { + $filter['type'] = 'entry_meta'; + $filter['choices'] = GFCommon::get_entry_payment_statuses_as_choices(); + } + if ( 'payment_status' === $field['field'] ) { $filter['type'] = 'entry_meta'; $filter['choices'] = GFCommon::get_entry_payment_statuses_as_choices(); diff --git a/readme.txt b/readme.txt index 4eaccde6b..3d959772a 100644 --- a/readme.txt +++ b/readme.txt @@ -23,8 +23,13 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h = develop = -#### 🐛 Fixed -* Searching for products only worked for price ranges. +* Added: "Read Status" field to display whether an entry has been read or not. Previously, the status was output as either 1 or 0. Now, you can customize the labels for "Read" and "Unread" statuses. + - You can now sort a View by "Read Status". + +__Developer Updates:__ + +* Added `gk/gravityview/field/is-read/print-script` filter to modify whether to print the script in the frontend that marks an entry as "Read". +* Added `gk/gravityview/field/is-read/read-label` filter to change field "Read" label. = 2.23 on May 17, 2024 = @@ -33,9 +38,6 @@ This update adds support for Nested Forms' entry meta, addresses several bugs, i #### 🚀 Added * Support for Gravity Wiz's Gravity Forms Nested Forms entry meta (parent form and entry IDs, child form field ID) in the View editor and merge tags. -#### ✨ Improved -* The "Add All Fields" button in the View editor now adds fields in their correct form order. - #### 🐛 Fixed * Export link View widget would cause a fatal error during multi-word searches. * Fatal error when the search bar is configured with a Gravity Flow field and the Gravity Flow plugin is not active.