Home / Export Snippets From the Snippets List
Duplicate Snippet

Embed Snippet on Your Site

Export Snippets From the Snippets List

Adds an "Export" link to each snippet and an "Export" bulk action in the WPCode list of snippets.
<10
Code Preview
php
<?php
add_filter(
	'wpcode_code_snippets_row_actions',
	function ( $actions, $snippet, $view ) {
		if ( 'trash' === $view || ! current_user_can( 'wpcode_edit_php_snippets' ) ) {
			return $actions;
		}
		$url = wp_nonce_url(
			add_query_arg(
				array(
					'page'       => 'wpcode',
					'action'     => 'export',
					'snippet_id' => absint( $snippet->ID ),
				),
				admin_url( 'admin.php' )
			),
			'wpcode_export_nonce'
		);
		$actions['export'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html__( 'Export', 'insert-headers-and-footers' ) );
		return $actions;
	},
	10,
	3
);
add_filter(
	'wpcode_snippets_bulk_actions',
	function ( $bulk_actions ) {
		if ( isset( $bulk_actions['trash'] ) && current_user_can( 'wpcode_edit_php_snippets' ) ) {
			$bulk_actions['export'] = __( 'Export', 'insert-headers-and-footers' );
		}
		return $bulk_actions;
	}
);
add_action(
	'load-toplevel_page_wpcode',
	function () {
		// phpcs:disable WordPress.Security.NonceVerification.Recommended
		$action = isset( $_GET['action'] ) ? sanitize_key( $_GET['action'] ) : '';
		if ( '-1' === $action || '' === $action ) {
			$action = isset( $_GET['action2'] ) ? sanitize_key( $_GET['action2'] ) : '';
		}
		$ids = isset( $_GET['snippet_id'] ) ? array_map( 'absint', (array) $_GET['snippet_id'] ) : array();
		// phpcs:enable WordPress.Security.NonceVerification.Recommended
		if ( 'export' !== $action || empty( $ids ) || empty( $_GET['_wpnonce'] ) ) {
			return;
		}
		$nonce = sanitize_key( $_GET['_wpnonce'] );
		if ( ! wp_verify_nonce( $nonce, 'bulk-wpcode-snippets' ) && ! wp_verify_nonce( $nonce, 'wpcode_export_nonce' ) ) {
			wp_die( esc_html__( 'Security check failed.', 'insert-headers-and-footers' ) );
		}
		// Same capability as Tools > Export, since the file holds the snippet code.
		if ( ! current_user_can( 'wpcode_edit_php_snippets' ) ) {
			wp_die( esc_html__( 'You are not allowed to export snippets.', 'insert-headers-and-footers' ) );
		}
		$export = array();
		foreach ( $ids as $id ) {
			if ( 'wpcode' !== get_post_type( $id ) || ! current_user_can( 'edit_post', $id ) ) {
				continue;
			}
			$snippet                          = new WPCode_Snippet( $id );
			$snippet_data                     = $snippet->get_data_for_caching();
			$snippet_data['tags']             = $snippet->get_tags();
			$snippet_data['note']             = $snippet->get_note();
			$snippet_data['cloud_id']         = $snippet->get_cloud_id();
			$snippet_data['custom_shortcode'] = $snippet->get_custom_shortcode();
			$export[] = apply_filters( 'wpcode_export_snippet_data', $snippet_data, $snippet );
		}
		if ( empty( $export ) ) {
			return;
		}
		$name = 1 === count( $export ) ? 'wpcode-snippet-' . $export[0]['id'] : 'wpcode-snippets-export';
		nocache_headers();
		header( 'Content-Type: application/json; charset=utf-8' );
		header( 'Content-Disposition: attachment; filename=' . $name . '-' . current_time( 'Y-m-d' ) . '.json' );
		echo wp_json_encode( $export );
		exit;
	},
	5
);

Comments

Add a Comment