moving-leads-form / src / admin / Controller.php
Controller.php
Raw
<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       http://example.com
 * @since      1.0.0
 */

namespace Formine\admin;

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @author     Your Name <email@example.com>
 */
class Controller
{

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $formine    The ID of this plugin.
	 */
	private $formine;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $formine       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct($formine, $version)
	{

		$this->formine = $formine;
		$this->version = $version;
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles()
	{
		wp_enqueue_style($this->formine, plugin_dir_url(__FILE__) . 'css/plugin-name-admin.css', array(), $this->version, 'all');
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts()
	{
		wp_enqueue_script($this->formine, plugin_dir_url(__FILE__) . 'js/plugin-name-admin.js', array('jquery'), $this->version, false);
	}

	/**
	 * Register the custom post type used to store the customer
	 * data from form submissions.
	 */
	public function create_custom_post_type()
	{
		$args = array(
			'labels' => [
				'name' => 'Formine Submissions',
				'singular_name' => 'Formine Submission'
			],
			'menu_icon' => 'dashicons-media-text',
			'capability' => 'manage_options',
			'public' => false,
			'show_ui' => true,
			'show_in_menu' => false,
			'show_in_nav_menus' => false,
			'show_in_rest' => true,
			'supports' => array('custom-fields'),
		);

		register_post_type('formine_submissions', $args);

		$form_schema = [
			'from' => ['type' => 'string'],
			'to' => ['type' => 'string'],
			'moveSize' => ['type' => 'string'],
			'moveDate' => ['type' => 'string'],
			'name' => ['type' => 'string'],
			'email' => ['type' => 'string'],
			'phone' => ['type' => 'string'],
		];

		if (!current_user_can('edit_posts')) {
			return;
		}

		register_post_meta(
			'formine_submissions',
			'form_data',
			[
				'single' => true,
				'type' => 'object',
				'show_in_rest' => [
					'schema' => [
						'type' => 'object',
						'properties' => $form_schema
					]
				]
			]
		);
	}

	public function add_admin_menu()
	{
		add_menu_page(
			'Mover Marketing',
			'Submissions',
			'manage_options',
			'formine-plugin',
			[$this, 'load_plugin_page'],
			'dashicons-smiley',
			4
		);
	}

	public function load_plugin_page()
	{
		// load page templates separately
		require_once __DIR__ . '/admin-template.php';
	}
}