Skip to content

Commit

Permalink
Update docblocks, filter names, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcasual committed May 27, 2024
1 parent f583d3f commit 4077c2a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 66 deletions.
139 changes: 76 additions & 63 deletions includes/fields/class-gravityview-field-is-read.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php
/**
* @file class-gravityview-field-is-read.php
* @package GravityView
* @file class-gravityview-field-is-read.php
* @since TBD
* @subpackage includes\fields
* @since TODO
* @package GravityView
*/

use GV\Field;
use GV\Template_Context;
use GV\View;

/**
* Field to display whether the entry has been read.
*
* @since TODO
* @since TBD
*/
class GravityView_Field_Is_Read extends GravityView_Field {

var $name = 'is_read';

var $is_searchable = true;
Expand All @@ -32,10 +35,11 @@ class GravityView_Field_Is_Read extends GravityView_Field {
var $is_sortable = true;

/**
* GravityView_Field_Is_Read constructor.
* Class constructor.
*
* @since TBD
*/
public function __construct() {

$this->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' );
Expand All @@ -46,7 +50,9 @@ public function __construct() {
}

/**
* Don't override Gravity Forms entry meta, even though it's a meta field.
* Prevents overriding Gravity Forms entry meta, even though it's a meta field.
*
* @since TBD
*
* @param array $entry_meta Existing entry meta.
*
Expand All @@ -57,16 +63,22 @@ public function add_entry_meta( $entry_meta ) {
}

/**
* Add hooks for the field.
* Adds field hooks.
*
* @since TBD
*/
private function add_hooks() {
/** @see \GV\Field::get_value_filters */
add_filter( 'gravityview/field/is_read/value', [ $this, 'get_value' ], 10, 6 );
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' ),
Expand All @@ -85,21 +97,17 @@ public function field_options( $field_options, $template_id, $field_id, $context
}

/**
* Display the value based on the field settings
* Displays the value based on the field settings.
*
* @since 2.0
*
* @param string $value The value.
* @param \GV\Field $field The field we're doing this for.
* @param \GV\View $view The view for this context if applicable.
* @param \GV\Source $source The source (form) for this context if applicable.
* @param \GV\Entry $entry The entry for this context if applicable.
* @param \GV\Request $request The request for this context if applicable.
* @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, $source, $entry, $request ) {

public function get_value( $value, $field, $view ) {
if ( empty( $value ) ) {
return \GV\Utils::get( $field, 'is_unread_label', esc_html__( 'Unread', 'gk-gravityview' ) );
}
Expand All @@ -108,35 +116,42 @@ public function get_value( $value, $field, $view, $source, $entry, $request ) {
}

/**
* Get the label for "Read" for a field.
* Returns the field's "Read" label.
*
* @param \GV\Field $field The field.
* @param \GV\View $view The View.
* @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 = \GV\Utils::get( $field, 'is_read_label', esc_html__( 'Read', 'gk-gravityview' ) );

/**
* @filter `gk/gravityview/field/is_read/label` Modify the label for the "Read" status.
* @since TODO
* Modify the "Read" label.
*
* @filter `gk/gravityview/field/is-read/read-label`
*
* @since TBD
*
* @param string $label The label.
* @param \GV\Field $field The field.
* @param \GV\View $view The View.
* @param Field $field The field.
* @param View $view The View.
*/
$label = apply_filters( 'gk/gravityview/field/is_read/is_read_label', $label, $field, $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.
*
* @param \GV\Template_Context $context The context.
* @since TBD
*
* @param Template_Context $context The context.
*
* @return \GV\Field|null The field or null if not found.
* @return Field|null The field or null if not found.
*/
protected function get_field_from_context( $context ) {
foreach ( $context->fields->all() as $field ) {
Expand All @@ -149,74 +164,72 @@ protected function get_field_from_context( $context ) {
}

/**
* Add JS to the bottom of the View if there is a read field and user has `gravityview_edit_entries` cap
* Adds JS to the bottom of the View if there is a read field and user has `gravityview_edit_entries` capability.
*
* @param \GV\Template_Context $context The template context
* @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;
}

/**
* @filter `gk/gravityview/field/is_read/print_script` Disable the script that marks the entry as read.
* @since TODO
* @param bool $print_script Should the script be printed? Default: true.
* @param \GV\Template_Context $context The template context.
* 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 ) ) {
if ( ! apply_filters( 'gk/gravityview/field/is-read/print-script', true, $context ) ) {
return;
}

$entry = gravityview()->request->is_entry();

if ( ! $entry ) {
if ( empty( $entry['is_read'] ) ) {
return;
}

if ( ! empty( $entry['is_read'] ) ) {
return;
}

$field = $this->get_field_from_context( $context, $context );
$field = $this->get_field_from_context( $context );
$read_label = $this->get_is_read_label( $field, $context->view );
?>
<script>
jQuery( document ).ready( function ( $ ) {
jQuery( function ( $ ) {
const entryId = <?php echo (int) $context->entry->ID; ?>;
const isReadField = $( '[class*=is_read]' );
const isReadFieldLabel = '<?php echo esc_html( $read_label ); ?>';

var entry_id = <?php echo (int) $context->entry->ID; ?>;
read_field = $('[class*=is_read]');
read_label = '<?php echo esc_html( $read_label ); ?>';

$.ajax({
type: "POST",
$.ajax( {
type: 'POST',
url: "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>",
data: {
action: 'rg_update_lead_property',
rg_update_lead_property: '<?php echo wp_create_nonce( 'rg_update_lead_property' ); ?>',
lead_id: entry_id,
lead_id: entryId,
name: 'is_read',
value: 1
}
}).done(function() {
if(read_field.parents('tbody').length > 0){
read_field.find('td').text(read_label);
}else{
read_field.text(read_label);
} ).done( function () {
if ( isReadField.parents( 'tbody' ).length > 0 ) {
isReadField.find( 'td' ).text( isReadFieldLabel );
} else {
isReadField.text( isReadFieldLabel );
}
})
.fail(function() {
} )
.fail( function () {
alert(<?php echo json_encode( __( 'There was an error updating the entry.', 'gk-gravityview' ) ); ?>);
});
});
} );
} );
</script>
<?php
}

}

new GravityView_Field_Is_Read();
7 changes: 4 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

= develop =

* Added: A "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"
* 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 on the front-end that marks an entry as "Read".
* 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 =

Expand Down

0 comments on commit 4077c2a

Please sign in to comment.