Source: controller/admin-scripts.php

<?php

namespace wpbuddy\rich_snippets;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly


/**
 * Class Admin_Scripts.
 *
 * Enqueues scripts and styles.
 *
 * @package wpbuddy\rich_snippets
 *
 * @since   2.0.0
 */
final class Admin_Scripts_Controller {

	/**
	 * The instance.
	 *
	 * @var Admin_Scripts_Controller
	 *
	 * @since 2.0.0
	 */
	protected static $instance = null;


	/**
	 * If this instance has been initialized already.
	 *
	 * @var bool
	 */
	protected $initialized = false;

	/**
	 * Get the singleton instance.
	 *
	 * Creates a new instance of the class if it does not exists.
	 *
	 * @return   Admin_Scripts_Controller
	 *
	 * @since 2.0.0
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self;
		}

		if ( ! self::$instance->initialized ) {
			self::$instance->init();
		}

		return self::$instance;
	}


	/**
	 * Magic function for cloning.
	 *
	 * Disallow cloning as this is a singleton class.
	 *
	 * @since 2.0.0
	 */
	protected function __clone() {
	}


	/**
	 * Magic method for setting upt the class.
	 *
	 * Disallow external instances.
	 *
	 * @since 2.0.0
	 */
	protected function __construct() {
	}


	/**
	 * Init and register.
	 *
	 * @since 2.2.0
	 */
	public function init() {

		if ( $this->initialized ) {
			return;
		}

		/**
		 * Register Styles
		 */

		wp_register_style(
			'wpb-rs-admin-snippets',
			plugins_url( 'css/admin-snippets.css', rich_snippets()->get_plugin_file() ),
			array(),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'css/admin-snippets.css' )
		);

		wp_register_style(
			'wpb-rs-admin-errors',
			plugins_url( 'css/admin-errors.css', rich_snippets()->get_plugin_file() ),
			array( 'wpb-rs-admin-snippets' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'css/admin-errors.css' )
		);

		wp_register_style(
			'wpb-rs-admin-posts',
			plugins_url( 'css/admin-posts.css', rich_snippets()->get_plugin_file() ),
			array( 'wpb-rs-admin-snippets' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'css/admin-posts.css' )
		);

		wp_register_style(
			'wpb-rs-admin-posts-overwrite',
			plugins_url( 'css/admin-posts-forms.css', rich_snippets()->get_plugin_file() ),
			array( 'wpb-rs-admin-errors' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'css/admin-posts-forms.css' )
		);


		/**
		 * Register scripts
		 */

		wp_register_script(
			'wpb-rs-admin-errors',
			plugins_url( 'js/admin-errors.js', rich_snippets()->get_plugin_file() ),
			array( 'jquery' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/admin-errors.js' )
		);

		wp_register_script(
			'wpb-rs-admin-snippets',
			plugins_url( 'js/admin-snippets.js', rich_snippets()->get_plugin_file() ),
			array(
				'wpb-rs-fields',
				'wpb-rs-admin-errors',
				'jquery',
				'underscore',
			),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/admin-snippets.js' )
		);

		wp_register_script(
			'wpb-rs-admin-posts',
			plugins_url( 'js/admin-posts.js', rich_snippets()->get_plugin_file() ),
			array( 'wpb-rs-admin-snippets', 'jquery' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/admin-posts.js' )
		);

		wp_register_script(
			'wpb-rs-admin-posts-overwrite',
			plugins_url( 'js/admin-posts-overwrite.js', rich_snippets()->get_plugin_file() ),
			array( 'wpb-rs-fields', 'wpb-rs-admin-errors', 'jquery' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/admin-posts-overwrite.js' )
		);

		wp_register_script(
			'wpb-rs-fields',
			plugins_url( 'js/fields.js', rich_snippets()->get_plugin_file() ),
			array( 'jquery' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/fields.js' )
		);

		wp_register_script(
			'wpb-rs-admin-rating',
			plugins_url( 'js/admin-rating.js', rich_snippets()->get_plugin_file() ),
			array( 'jquery' ),
			filemtime( plugin_dir_path( rich_snippets()->get_plugin_file() ) . 'js/admin-rating.js' ),
			true
		);

		$this->initialized = true;
	}


	/**
	 * Enqueue snippet styles.
	 *
	 * @since 2.0.0
	 */
	public function enqueue_snippets_styles() {

		wp_enqueue_style( 'wpb-rs-admin-snippets' );

		wp_enqueue_style( 'wpb-rs-admin-errors' );

		/**
		 * Snippet Styles Action.
		 *
		 * Allows plugins to enqueue custom styles on the snippets screen.
		 *
		 * @hook  wpbuddy/rich_snippets/schemas/styles
		 *
		 * @since 2.0.0
		 */
		do_action( 'wpbuddy/rich_snippets/schemas/styles' );
	}


	/***
	 * Enqueue snippet scripts.
	 *
	 * @since 2.0.0
	 */
	public function enqueue_snippets_scripts() {

		$this->enqueue_script_snippets();
	}


	/**
	 * Enqueue snippets scripts.
	 *
	 * @since 2.0.0
	 */
	private function enqueue_script_snippets() {

		wp_enqueue_script( 'wpb-rs-admin-snippets' );

		wp_add_inline_script(
			'wpb-rs-admin-snippets',
			"var WPB_RS_ADMIN = " . \json_encode( $this->get_admin_snippets_script_data() ) . ";",
			'before'
		);

		wp_enqueue_script( 'wpb-rs-admin-errors' );
	}


	/**
	 * Enqueue scripts for singular posts.
	 *
	 * @since 2.0.0
	 */
	private function enqueue_scripts_posts() {

		wp_enqueue_script( 'wpb-rs-admin-posts' );

		wp_add_inline_script(
			'wpb-rs-admin-posts',
			"var WPB_RS_POSTS = " . \json_encode( $this->get_admin_posts_script_data() ) . ";",
			'before'
		);

	}


	/**
	 * Enqueue posts forms scripts for singular posts.
	 *
	 * @since 2.2.0
	 */
	private function enqueue_scripts_posts_overwrite() {

		wp_enqueue_script( 'wpb-rs-admin-posts-overwrite' );

		wp_add_inline_script(
			'wpb-rs-admin-posts-overwrite',
			"var WPB_RS_POSTS_FORMS = " . \json_encode( $this->get_admin_posts_overwrite_script_data() ) . ";",
			'before'
		);
	}


	/**
	 * Returns an object of data needed by admin snippet script.
	 *
	 * @return \stdClass
	 * @since 2.0.0
	 *
	 */
	private function get_admin_snippets_script_data(): \stdClass {

		global $post;

		$post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0;

		$o                 = new \stdClass();
		$o->nonce          = wp_create_nonce( 'wp_rest' );
		$o->rest_url       = untrailingslashit( rest_url( 'wpbuddy/rich_snippets/v1' ) );
		$o->i18n           = new \stdClass();
		$o->i18n->expand   = __( 'Expand all', 'rich-snippets-schema' );
		$o->i18n->collapse = __( 'Collapse all', 'rich-snippets-schema' );

		if ( ! empty( $post_id ) ) {
			$o->post_id = $post_id;
		}

		return $o;
	}


	/**
	 * Returns an object of data needed by admin posts script.
	 *
	 * @return \stdClass
	 * @since 2.0.0
	 *
	 */
	private function get_admin_posts_script_data(): \stdClass {

		global $post;

		$post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0;

		$o           = new \stdClass();
		$o->nonce    = wp_create_nonce( 'wp_rest' );
		$o->rest_url = untrailingslashit( rest_url( 'wpbuddy/rich_snippets/v1' ) );

		if ( ! empty( $post_id ) ) {
			$o->post_id = $post_id;
		}

		return $o;
	}


	/**
	 * Returns an object of data needed by admin posts forms script.
	 *
	 * @return \stdClass
	 * @since 2.2.0
	 *
	 */
	private function get_admin_posts_overwrite_script_data(): \stdClass {

		global $post;

		$post_id = is_a( $post, 'WP_Post' ) ? $post->ID : 0;

		$o                          = new \stdClass();
		$o->nonce                   = wp_create_nonce( 'wp_rest' );
		$o->rest_url                = untrailingslashit( rest_url() );
		$o->i18n                    = new \stdClass();
		$o->i18n->save              = __( 'Save', 'rich-snippets-schema' );
		$o->i18n->saved             = __( 'Saved!', 'rich-snippets-schema' );
		$o->i18n->last_element_warn = __( 'This is the last property of this type. Really want to delete it?', 'rich-snippets-schema' );

		if ( ! empty( $post_id ) ) {
			$o->post_id = $post_id;
		}

		return $o;
	}


	/**
	 * Enqueue posts scripts.
	 *
	 * @since 2.0.0
	 */
	public function enqueue_posts_scripts() {

		$this->enqueue_scripts_posts();

		$this->enqueue_styles_posts();
	}


	/**
	 * Enqueue posts forms scripts.
	 *
	 * @since 2.2.0
	 */
	public function enqueue_posts_forms_scripts() {

		$this->enqueue_scripts_posts_overwrite();

		$this->enqueue_styles_posts_forms();
	}


	/**
	 * Enqueue CSS scripts for posts.
	 *
	 * @since 2.0.0
	 */
	private function enqueue_styles_posts() {

		wp_enqueue_style( 'wpb-rs-admin-posts' );

		/**
		 * Snippet Post Styles Action.
		 *
		 * Allows plugins to enqueue custom styles on the posts screen.
		 *
		 * @hook  wpbuddy/rich_snippets/posts/styles
		 *
		 * @since 2.0.0
		 */
		do_action( 'wpbuddy/rich_snippets/posts/styles' );
	}


	/**
	 * Enqueue CSS scripts for posts forms.
	 *
	 * @since 2.2.0
	 */
	private function enqueue_styles_posts_forms() {

		wp_enqueue_style( 'wpb-rs-admin-posts-overwrite' );

		/**
		 * Snippet Styles Post Forms.
		 *
		 * Allows plugins to enqueue custom styles on the snippets post forms screen.
		 *
		 * @hook  wpbuddy/rich_snippets/posts_forms/styles
		 *
		 * @since 2.2.0
		 */
		do_action( 'wpbuddy/rich_snippets/posts_forms/styles' );
	}


	/**
	 * Rating script data.
	 *
	 * @return \stdClass
	 * @since 2.9.0
	 *
	 */
	private function get_admin_rating_script_data() {
		$o           = new \stdClass();
		$o->nonce    = wp_create_nonce( 'wp_rest' );
		$o->rest_url = untrailingslashit( rest_url( 'wpbuddy/rich_snippets/v1' ) );

		$o->steps = [
			10 => [
				'text'    => __( 'Do you want to get free LIFETIME updates for SNIP, the Rich Snippets & Structured Data Plugin?', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'No', 'rich-snippets-schema' ),
						'next'  => 50,
					],
					[
						'label' => __( 'Yes, of course! 👍', 'rich-snippets-schema' ),
						'next'  => 15,
					],
				],
			],
			15 => [
				'text'    => __( 'Please rate this plugin on CodeCanyon. It only takes 30 seconds and it ensures ongoing sales and that programmers are properly paid for their work.', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Let\'s do this! 🤘', 'rich-snippets-schema' ),
						'next'  => 40,
						'link'  => 'https://codecanyon.net/downloads#item-3464341',
					],
					[
						'label' => __( 'I don\'t have time.', 'rich-snippets-schema' ),
						'next'  => 20,
					],
				],
			],
			20 => [
				'text'    => __( 'Without any further sales the plugin will not get updates anymore. Wouldn\'t you like to avoid that problem too?', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Yes, how can I help?', 'rich-snippets-schema' ),
						'next'  => 30,
					],
					[
						'label' => __( 'What\'s the worst case scenario?', 'rich-snippets-schema' ),
						'next'  => 50,
					]
				],
			],
			30 => [
				'text'    => __( 'Would you like to help in just 30 seconds? I need your 5-Star-Rating on CodeCanyon.', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Of course I want to help and rate now!', 'rich-snippets-schema' ),
						'next'  => 40,
						'link'  => 'https://codecanyon.net/downloads#item-3464341',
					],
					[
						'label' => __( 'I can\'t!', 'rich-snippets-schema' ),
						'next'  => 60,
					]
				],
			],
			40 => [
				'text'    => __( 'Did it work? Please note that you need to be logged-in to CodeCanyon.', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Didn\'t work. But I\'m logged in now. Try again.', 'rich-snippets-schema' ),
						'link'  => 'https://codecanyon.net/downloads#item-3464341',
						'next'  => 40,
					],
					[
						'label' => __( 'Yeah, I rated! 💪', 'rich-snippets-schema' ),
						'next'  => 70,
					]
				],
			],
			50 => [
				'text'    => __( 'Search engines tend to change a lot. Especially when it comes to structured data. Old snippets can lead to a loss of visibility in search engines. Do you want to risk that?', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'What can I do to stay updated?', 'rich-snippets-schema' ),
						'next'  => 30,
					],
					[
						'label' => __( 'I don\'t care.', 'rich-snippets-schema' ),
						'close' => true,
					]
				],
			],
			60 => [
				'text'    => __( 'If in case you cannot rate the plugin with 5 stars. Why not add a feature request? This also helps make the plugin even better!', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Show me the feature-request-form', 'rich-snippets-schema' ),
						'link'  => admin_url( 'admin.php?page=rich-snippets-support' )
					],
					[
						'label' => __( 'Remind me later', 'rich-snippets-schema' ),
						'close' => true,
					]
				],
			],
			70 => [
				'text'    => __( 'Awesome! You\'re my hero of the day! 🙌', 'rich-snippets-schema' ),
				'buttons' => [
					[
						'label' => __( 'Close', 'rich-snippets-schema' ),
						'close' => true,
					]
				],
			],
		];

		return $o;
	}


	/**
	 * Enqueues Rating scripts.
	 *
	 * @since 2.9.0
	 */
	public function enqueue_rating_scripts() {

		wp_enqueue_script( 'wpb-rs-admin-rating' );

		wp_add_inline_script(
			'wpb-rs-admin-rating',
			"var WPB_RS_ADMIN_RATING = " . \json_encode( $this->get_admin_rating_script_data() ) . ";",
			'before'
		);

		/**
		 * Rating Scripts Action.
		 *
		 * Allows plugins to enqueue custom scripts after rating scripts have been enqueued.
		 *
		 * @hook  wpbuddy/rich_snippets/rating/scripts
		 *
		 * @since 2.9.0
		 */
		do_action( 'wpbuddy/rich_snippets/rating/scripts' );
	}

}