Archive Entry Link

Add an Archive Link action to the Entries Table. Only users who can edit an entry can archive it. Requires GP Entry Blocks 1.0-beta-2.38 or newer and GF Entry Archive.

Instructions

  1. Install this snippet with your preferred snippet management solution. https://gravitywiz.com/documentation/managing-snippets/

  2. Edit your Entries Table block and add “Archive Link” to its Summary Columns.

  3. Optionally customize the Link Label in the column settings. The default label is “Archive”.

Code

Filename: gpeb-archive-entry-action.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
 * Gravity Perks // Entry Blocks // Archive Entry Link
 * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
 *
 * Add an Archive Link action to the Entries Table. Only users who can edit an entry can archive it.
 * Requires GP Entry Blocks 1.0-beta-2.38 or newer and GF Entry Archive.
 *
 * Instructions:
 *
 * 1. Install this snippet with your preferred snippet management solution.
 *    https://gravitywiz.com/documentation/managing-snippets/
 *
 * 2. Edit your Entries Table block and add "Archive Link" to its Summary Columns.
 *
 * 3. Optionally customize the Link Label in the column settings. The default label is "Archive".
 */
class GPEB_Archive_Entry_Link {

	public function __construct() {
		add_action( 'init', array( $this, 'init' ) );
	}

	public function init() {
		// Without GF Entry Archive, archived entries would be hidden everywhere with no way to restore them.
		if ( ! function_exists( 'gp_entry_blocks' ) || ! function_exists( 'gf_entry_archive' ) ) {
			return;
		}

		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_script' ) );
		add_filter( 'gpeb_table_row_cell_content', array( $this, 'render_archive_link' ), 10, 3 );
		add_filter( 'gpeb_cleaned_current_url', array( $this, 'remove_archive_entry_query_arg' ) );
		add_action( 'template_redirect', array( $this, 'archive_entry' ) );
	}

	public function remove_archive_entry_query_arg( $url ) {
		return remove_query_arg( 'archive_entry', $url );
	}

	public function enqueue_editor_script() {
		wp_register_script( 'gpeb-archive-entry-link', false, array( 'wp-hooks', 'wp-i18n' ), '0.1', true );
		wp_enqueue_script( 'gpeb-archive-entry-link' );
		wp_add_inline_script( 'gpeb-archive-entry-link', <<<'JS'
wp.hooks.addFilter(
	'gpeb_entries_table_action_items',
	'gpeb/archive-entry-link',
	function( actionItems, context ) {
		return actionItems.concat( {
			type: 'archive-link',
			label: wp.i18n.__( 'Archive Link', 'gp-entry-blocks' ),
			group: wp.i18n.__( 'Actions', 'gp-entry-blocks' ),
			customOptions: context.LinkColumnOptions,
		} );
	}
);
JS
		);
	}

	public function render_archive_link( $content, $column, $row ) {
		if ( $column->type !== 'archive-link' ) {
			return $content;
		}

		// Only active entries can be archived. Blocks filtered by Status may also list trashed, spam or archived entries.
		if ( rgar( $row->entry, 'status' ) !== 'active' || ! gp_entry_blocks()->permissions->can_current_user_edit_entry( $row->entry ) ) {
			return '';
		}

		$label = rgar( $column->meta, 'linkLabel' );
		$label = $label ? $label : __( 'Archive', 'gp-entry-blocks' );
		$url   = wp_nonce_url(
			add_query_arg( 'archive_entry', $row->entry['id'], \GP_Entry_Blocks\cleaned_current_url( false ) ),
			'archive_entry_' . $row->entry['id']
		);

		return sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( $label ) );
	}

	public function archive_entry() {
		$entry_id = rgget( 'archive_entry' );
		$nonce    = rgget( '_wpnonce' );

		if ( ! is_scalar( $entry_id ) || ! ctype_digit( (string) $entry_id ) || ! is_string( $nonce ) ) {
			return;
		}

		$entry_id = absint( $entry_id );

		if ( ! $entry_id || ! wp_verify_nonce( $nonce, 'archive_entry_' . $entry_id ) ) {
			return;
		}

		$entry = GFAPI::get_entry( $entry_id );

		if ( ! $entry || is_wp_error( $entry ) || rgar( $entry, 'status' ) !== 'active' || ! gp_entry_blocks()->permissions->can_current_user_edit_entry( $entry ) ) {
			return;
		}

		// GF Entry Archive stores archived entries with the "archived" status.
		if ( false === GFAPI::update_entry_property( $entry_id, 'status', 'archived' ) ) {
			wp_die( esc_html__( 'The entry could not be archived. Please try again.', 'gp-entry-blocks' ) );
		}

		wp_safe_redirect( remove_query_arg( array( 'archive_entry', '_wpnonce' ) ) );
		exit;
	}

}

new GPEB_Archive_Entry_Link();

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.