From 53b2267fc529c4d1746adf7439870c98f9cf8bd0 Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sat, 11 Jun 2022 18:31:07 -0700
Subject: [PATCH] Register meta

---
 ...external-permalinks-redux-block-editor.php | 50 +++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/inc/class-external-permalinks-redux-block-editor.php b/inc/class-external-permalinks-redux-block-editor.php
index 2feb7ee..b6b737c 100644
--- a/inc/class-external-permalinks-redux-block-editor.php
+++ b/inc/class-external-permalinks-redux-block-editor.php
@@ -41,9 +41,59 @@ class External_Permalinks_Redux_Block_Editor {
 	 * @return void
 	 */
 	protected function _setup() {
+		add_action( 'rest_api_init', array( $this, 'register_meta' ) );
 		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue' ) );
 	}
 
+	/**
+	 * Register meta for access in block editor.
+	 *
+	 * @return void
+	 */
+	public function register_meta() {
+		global $wp_version;
+
+		if (
+			! function_exists( 'register_meta' )
+			|| version_compare( $wp_version, '4.6.0', '<' )
+		) {
+			return;
+		}
+
+		register_meta(
+			'post',
+			external_permalinks_redux::get_instance()->meta_key_target,
+			array(
+				'default'           => '',
+				'description'       => __(
+					'Redirect destination',
+					'external-permalinks-redux'
+				),
+				'type'              => 'string',
+				'sanitize_callback' => 'esc_url_raw',
+				'show_in_rest'      => true,
+				'single'            => true,
+			)
+		);
+
+		register_meta(
+			'post',
+			external_permalinks_redux::get_instance()->meta_key_type,
+			array(
+				// Matches fallback in `external_permalinks_redux::get_redirect_data()`
+				'default'           => 302,
+				'description'       => __(
+					'Redirect status code',
+					'external-permalinks-redux'
+				),
+				'type'              => 'integer',
+				'sanitize_callback' => 'absint',
+				'show_in_rest'      => true,
+				'single'            => true,
+			)
+		);
+	}
+
 	/**
 	 * Enqueue block-editor script.
 	 *
-- 
GitLab