15 if ( ! defined(
'WPINC' ) ) {
52 self::$file = plugin_dir_path( __FILE__ );
70 $dir = trailingslashit( self::$file );
72 $filename = $dir .
'class-delete-entry-' . $component .
'.php';
73 $classname =
'GravityView_Delete_Entry_' . str_replace(
' ',
'_', ucwords( str_replace(
'-',
' ', $component ) ) );
76 require_once $filename;
78 $this->instances[ $component ] =
new $classname( $this );
79 $this->instances[ $component ]->load();
87 add_action(
'wp', array( $this,
'process_delete' ), 10000 );
89 add_action(
'gravityview_before', array( $this,
'maybe_display_message' ) );
92 add_filter(
'gravityview_template_paths', array( $this,
'add_template_path' ) );
94 add_action(
'gravityview/edit-entry/publishing-action/after', array( $this,
'add_delete_button' ), 10, 4 );
96 add_action(
'gravityview/delete-entry/deleted', array( $this,
'process_connected_posts' ), 10, 2 );
97 add_action(
'gravityview/delete-entry/trashed', array( $this,
'process_connected_posts' ), 10, 2 );
99 add_filter(
'gravityview/field/is_visible', array( $this,
'maybe_not_visible' ), 10, 3 );
101 add_filter(
'gravityview/api/reserved_query_args', array( $this,
'add_reserved_arg' ) );
128 if ( empty( self::$instance ) ) {
129 self::$instance =
new self();
132 return self::$instance;
148 if (
'delete_link' !==
$field->ID ) {
156 static $visibility_cache_for_view = array();
158 if ( ! is_null( $result = \
GV\Utils::get( $visibility_cache_for_view, $view->ID, null ) ) ) {
162 foreach ( $view->get_entries()->all() as
$entry ) {
163 if ( self::check_user_cap_delete_entry( $entry->as_entry(),
$field->as_configuration(), $view ) ) {
165 $visibility_cache_for_view[ $view->ID ] =
true;
170 $visibility_cache_for_view[ $view->ID ] =
false;
185 $file_paths[115] = self::$file;
197 _deprecated_function( __METHOD__,
'2.9.2' );
208 return sprintf(
'delete_%s', $entry_id );
229 if ( empty( $base ) ) {
230 gravityview()->log->error(
'Post ID does not exist: {post_id}', array(
'post_id' => $post_id ) );
245 $add_query_args = apply_filters(
'gravityview/delete-entry/add_query_args',
true );
251 $actionurl = add_query_arg(
253 'action' =>
'delete',
256 'view_id' => $view_id,
258 remove_query_arg(
'message', $base )
280 public function add_delete_button( $form = array(), $entry = array(), $view_id = null, $post_id = null ) {
283 if ( ! self::check_user_cap_delete_entry( $entry, array(), $view_id ) ) {
291 $show_delete_button = apply_filters(
'gravityview/delete-entry/show-delete-button',
true );
294 if ( ! $show_delete_button ) {
299 'class' =>
'btn btn-sm button button-small alignright pull-right btn-danger gv-button-delete',
300 'tabindex' => ( GFCommon::$tab_index ++ ),
301 'onclick' => self::get_confirm_dialog(),
306 $delete_label = __(
'Delete',
'Button label to delete an entry from the Edit Entry screen',
'gk-gravityview' );
309 $delete_label = $View->settings->get(
'action_label_delete', $delete_label );
312 $delete_label = GFCommon::replace_variables( $delete_label, $form, $entry );
333 $get_fields = wp_parse_args(
345 if (
'delete' !== $get_fields[
'action'] || empty( $get_fields[
'entry_id'] ) ) {
350 $valid_nonce_key = wp_verify_nonce( $get_fields[
'delete'], self::get_nonce_key( $get_fields[
'entry_id'] ) );
352 if ( ! $valid_nonce_key ) {
353 gravityview()->log->debug(
'Delete entry not processed: nonce validation failed.' );
367 $delete_redirect_base = esc_url_raw( remove_query_arg( array(
'action',
'gvid',
'entry_id' ) ) );
371 gravityview()->log->debug(
'Delete entry failed: there was no entry with the entry slug {entry_slug}', array(
'entry_slug' =>
$entry_slug ) );
373 $this->
_redirect_and_exit( $delete_redirect_base, __(
'The entry does not exist.',
'gk-gravityview' ),
'error' );
378 if ( is_wp_error( $has_permission ) ) {
379 $this->
_redirect_and_exit( $delete_redirect_base, $has_permission->get_error_message(),
'error' );
385 if ( is_wp_error( $delete_response ) ) {
386 $this->
_redirect_and_exit( $delete_redirect_base, $delete_response->get_error_message(),
'error' );
389 if ( (
int) $view->settings->get(
'delete_redirect' ) === self::REDIRECT_TO_URL_VALUE ) {
391 $form = GFAPI::get_form( $entry[
'form_id'] );
392 $redirect_url_setting = $view->settings->get(
'delete_redirect_url' );
393 $redirect_url = GFCommon::replace_variables( $redirect_url_setting, $form, $entry,
false,
false,
false,
'text' );
414 $delete_redirect_args = array(
416 'message' => $message,
419 $delete_redirect_args = array_filter( $delete_redirect_args );
427 $delete_redirect_args = apply_filters(
'gravityview/delete-entry/redirect-args', $delete_redirect_args );
429 $delete_redirect_url = add_query_arg( $delete_redirect_args,
$url );
431 if ( $safe_redirect ) {
432 wp_safe_redirect( $delete_redirect_url );
434 wp_redirect( $delete_redirect_url );
452 $delete_mode = apply_filters(
'gravityview/delete-entry/mode',
'delete' );
454 return (
'trash' === $delete_mode ) ?
'trash' :
'delete';
467 $entry_id = $entry[
'id'];
471 if (
'delete' === $mode ) {
473 gravityview()->log->debug(
'Starting delete entry: {entry_id}', array(
'entry_id' => $entry_id ) );
476 $delete_response = GFAPI::delete_entry( $entry_id );
478 if ( ! is_wp_error( $delete_response ) ) {
479 $delete_response =
'deleted';
487 do_action(
'gravityview/delete-entry/deleted', $entry_id, $entry );
490 gravityview()->log->debug(
'Delete response: {delete_response}', array(
'delete_response' => $delete_response ) );
494 gravityview()->log->debug(
'Starting trash entry: {entry_id}', array(
'entry_id' => $entry_id ) );
496 $trashed = GFAPI::update_entry_property( $entry_id,
'status',
'trash' );
500 $delete_response =
new WP_Error(
'trash_entry_failed', __(
'Moving the entry to the trash failed.',
'gk-gravityview' ) );
509 do_action(
'gravityview/delete-entry/trashed', $entry_id, $entry );
511 $delete_response =
'trashed';
514 gravityview()->log->debug(
' Trashed? {delete_response}', array(
'delete_response' => $delete_response ) );
517 return $delete_response;
531 if ( empty( $entry[
'post_id'] ) ) {
540 $delete_post = apply_filters(
'gravityview/delete-entry/delete-connected-post',
true );
542 if (
false === $delete_post ) {
546 $action = current_action();
548 if (
'gravityview/delete-entry/deleted' === $action ) {
549 $result = wp_delete_post( $entry[
'post_id'],
true );
551 $result = wp_trash_post( $entry[
'post_id'] );
554 if (
false === $result ) {
556 '(called by {action}): Error processing the Post connected to the entry.',
564 '(called by {action}): Successfully processed Post connected to the entry.',
582 if ( empty( $_GET[
'entry_id'] ) || empty( $_GET[
'delete'] ) ) {
586 $nonce_key = self::get_nonce_key( $_GET[
'entry_id'] );
588 $valid = wp_verify_nonce( $_GET[
'delete'], $nonce_key );
597 $valid = apply_filters(
'gravityview/delete-entry/verify_nonce', $valid, $nonce_key );
610 $confirm = __(
'Are you sure you want to delete this entry? This cannot be undone.',
'gk-gravityview' );
616 $confirm = apply_filters(
'gravityview/delete-entry/confirm-text', $confirm );
618 return 'return window.confirm(\'' . esc_js( $confirm ) .
'\');
'; 632 function user_can_delete_entry( $entry = array(), $view_id = null ) {
636 if ( ! $this->verify_nonce() ) {
637 $error = __( 'The link to
delete this entry is not valid; it may have expired.
', 'gk-
gravityview' ); 640 if ( ! self::check_user_cap_delete_entry( $entry, array(), $view_id ) ) { 641 $error = __( 'You
do not have permission to
delete this entry.
', 'gk-
gravityview' ); 644 if ( $entry['status
'] === 'trash
' ) { 645 if ( 'trash
' === $this->get_delete_mode() ) { 646 $error = __( 'The entry is already in the trash.
', 'gk-
gravityview' ); 648 $error = __( 'You cannot
delete the entry; it is already in the trash.
', 'gk-
gravityview' ); 652 // No errors; everything's fine here!
653 if ( empty( $error ) ) {
657 gravityview()->log->error(
'{error}', array(
'erorr' => $error ) );
659 return new WP_Error(
'gravityview-delete-entry-permissions', $error );
680 if ( ! $view instanceof \
GV\View ) {
683 $view_id = $view->ID;
686 $current_user = wp_get_current_user();
688 $entry_id = isset( $entry[
'id'] ) ? $entry[
'id'] : null;
691 if (
GVCommon::has_cap( array(
'gravityforms_delete_entries',
'gravityview_delete_others_entries' ), $entry_id ) ) {
693 gravityview()->log->debug(
'Current user has `gravityforms_delete_entries` or `gravityview_delete_others_entries` capability.' );
699 if ( ! empty(
$field ) ) {
702 if ( empty(
$field[
'allow_edit_cap'] ) ) {
704 gravityview()->log->error(
'Cannot read delete entry field caps', array(
'data' =>
$field ) );
712 if (
$field[
'allow_edit_cap'] !==
'read' ) {
717 gravityview()->log->debug(
'User {user_id} is not authorized to view delete entry link ', array(
'user_id' => $current_user->ID ) );
723 if ( ! isset( $entry[
'created_by'] ) ) {
725 gravityview()->log->error(
'Entry `created_by` doesn\'t exist.' );
730 $user_delete = $view->settings->get(
'user_delete' );
733 if ( $view && empty( $user_delete ) ) {
734 gravityview()->log->debug(
'User Delete is disabled. Returning false.' );
739 if ( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry[
'created_by'] ) ) {
741 gravityview()->log->debug(
'User {user_id} created the entry.', array(
'user_id' => $current_user->ID ) );
764 if ( empty( $_GET[
'status'] ) || ! self::verify_nonce() ) {
769 if ( isset( $_GET[
'view_id'] ) && intval( $_GET[
'view_id'] ) !== intval( $current_view_id ) ) {
778 if ( empty( $_GET[
'status'] ) || empty( $_GET[
'delete'] ) ) {
782 $status = esc_attr( $_GET[
'status'] );
784 $message_from_url = rawurldecode( stripslashes_deep( $message_from_url ) );
789 $class =
' gv-error error';
790 $error_message = __(
'There was an error deleting the entry: %s',
'gk-gravityview' );
791 $message = sprintf( $error_message, $message_from_url );
794 $message = __(
'The entry was successfully moved to the trash.',
'gk-gravityview' );
797 $message = __(
'The entry was successfully deleted.',
'gk-gravityview' );
808 $message = apply_filters(
'gravityview/delete-entry/message', esc_attr( $message ), $status, $message_from_url );
process_connected_posts( $entry_id=0, $entry=array())
Delete or trash a post connected to an entry.
gravityview_get_view_id()
Get the current View ID being rendered.
verify_nonce()
Is the current nonce valid for editing the entry?
static get_delete_link( $entry, $view_id=0, $post_id=null)
Generate a nonce link with the base URL of the current View embed.
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
user_can_delete_entry( $entry=array(), $view_id=null)
Check if the user can edit the entry.
static getInstance( $passed_post=NULL)
_redirect_and_exit( $url, $message='', $status='', $safe_redirect=true)
Redirects the user to a URL and exits.
load_components( $component)
Load other files related to Delete Entry functionality.
process_delete()
Handle the deletion request, if $_GET['action'] is set to "delete".
static generate_notice( $notice, $class='', $cap='', $object_id=null)
Display updated/error notice.
gravityview_get_link( $href='', $anchor_text='', $atts=array())
Generate an HTML anchor tag with a list of supported attributes.
gravityview_get_entry( $entry_slug, $force_allow_ids=false, $check_entry_display=true, $view=null)
Return a single entry object.
static from_entry( $entry)
Construct a instance from a Gravity Forms entry array.
static directory_link( $post_id=NULL, $add_query_args=true, $context=null)
Generate a URL to the Directory context.
static get_confirm_dialog()
Get the onclick attribute for the confirm dialogs that warns users before they delete an entry...
static check_user_cap_delete_entry( $entry, $field=array(), $view=0)
checks if user has permissions to view the link or delete a specific entry
maybe_not_visible( $visible, $field, $view)
Hide the field or not.
add_delete_button( $form=array(), $entry=array(), $view_id=null, $post_id=null)
Add a Delete button to the "#publishing-action" section of the Delete Entry form. ...
delete_or_trash_entry( $entry)
static by_id( $post_id)
Construct a instance from a post ID.
set_entry( $entry=null)
Make sure there's an entry.
static getInstance()
Return the instantiated class object.
add_template_path( $file_paths)
Include this extension templates path.
const REDIRECT_TO_URL_VALUE
The value of the delete_redirect option when the setting is to redirect to URL.
gravityview()
The main GravityView wrapper function.
gv_get_query_args()
Returns query parameters from $_GET with reserved internal GravityView keys removed.
static has_cap( $caps='', $object_id=null, $user_id=null)
Alias of GravityView_Roles_Capabilities::has_cap()
get_delete_mode()
Delete mode: permanently delete, or move to trash?
maybe_display_message( $current_view_id=0)
After processing delete entry, the user will be redirected to the referring View or embedded post/pag...
static get_nonce_key( $entry_id)
Generate a consistent nonce key based on the Entry ID.
const REDIRECT_TO_MULTIPLE_ENTRIES_VALUE
The value of the delete_redirect option when the setting is to redirect to Multiple Entries after del...
add_reserved_arg( $args)
Adds "delete" to the list of internal reserved query args.