unFocus / Scripts-n-Styles

Allows WordPress admin users the ability to add custom CSS and JavaScript directly to individual Post, Pages or custom post types.

Home Page:http://www.unFocus.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expose in REST API

jonferreira opened this issue · comments

Hi,

I'm working on importing 100s of pages to wordpress and some have in-line CSS/JS which I'll need to import.

I'm using the WP REST API to do this and I'd need to know how / if it's possible to use the REST API to "paste" my CSS/JS code snippets per post.
Thanks

OK clearly it isn't so I've registered _SnS to be exposed in REST but still struggling unfortunatelly

static function init() {
		if ( is_admin() && ! ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) ) {
			/*	NOTE: Setting the DISALLOW_UNFILTERED_HTML constant to
				true in the wp-config.php would effectively disable this
				plugin's admin because no user would have the capability.
			*/
			include_once( 'includes/class-sns-admin.php' );
			SnS_Admin::init();
		}
		//register_theme_directory( WP_PLUGIN_DIR . "/" . basename( dirname( __FILE__ ) ) . '/theme/' );
		add_action( 'plugins_loaded', array( __CLASS__, 'upgrade_check' ) );

		add_filter( 'body_class', array( __CLASS__, 'body_classes' ) );
		add_filter( 'post_class', array( __CLASS__, 'post_classes' ) );

		add_action( 'wp_head', array( __CLASS__, 'styles' ), 11 );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 11 );
		add_action( 'wp_head', array( __CLASS__, 'scripts_in_head' ), 11 );
		add_action( 'wp_footer', array( __CLASS__, 'scripts' ), 11 );

		add_action( 'plugins_loaded', array( __CLASS__, 'add_shortcodes' ) );
		add_action( 'widgets_init', array( __CLASS__, 'add_widget' ) );

		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register' ) );

		add_action( 'wp_print_styles', array( __CLASS__, 'theme_style' ) );
		add_action( 'wp_ajax_sns_theme_css', array( __CLASS__, 'theme_css' ) );
		add_action( 'wp_ajax_nopriv_sns_theme_css', array( __CLASS__, 'theme_css' ) );
		
		register_meta('post', '_SnS',
			[
				'sanitize_callback' => 'sanitize_array_field',
				'show_in_rest' => true,
				'single' => false
			]
		);
	}
	
	function sanitize_array_field( $meta_value ) {

		foreach ( (array) $meta_value as $k => $v ) {
			if ( is_array( $v ) ) {
				$meta_value[$k] =  sanitize_array_field( $v );
			} else {
				$meta_value[$k] = sanitize_text_field( $v );
			}
		}

		return $meta_value;

	}

And I get "array" instead of an actual Array

image