forms.php
144 lines
| 1 | <?php |
| 2 | namespace Wpmet\Libs; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | if ( ! class_exists( '\Wpmet\Libs\Forms' ) ) : |
| 7 | |
| 8 | class Forms { |
| 9 | |
| 10 | /** |
| 11 | * Member Variable |
| 12 | * |
| 13 | * @var instance |
| 14 | */ |
| 15 | private static $instance; |
| 16 | |
| 17 | /** |
| 18 | * Instance. |
| 19 | * |
| 20 | * Ensures only one instance of the plugin class is loaded or can be loaded. |
| 21 | * |
| 22 | * @since 2.6.3 |
| 23 | * @access public |
| 24 | * @static |
| 25 | * |
| 26 | * @return Init An instance of the class. |
| 27 | */ |
| 28 | public static function instance() { |
| 29 | |
| 30 | if ( is_null( self::$instance ) ) { |
| 31 | |
| 32 | self::$instance = new self(); |
| 33 | } |
| 34 | |
| 35 | return self::$instance; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Construct the plugin object. |
| 40 | * |
| 41 | * @since 2.6.3 |
| 42 | * @access public |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | |
| 46 | // register admin menus |
| 47 | add_action('admin_menu', [$this, 'register_sub_menu'], 999); |
| 48 | |
| 49 | // register js/ css |
| 50 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 51 | |
| 52 | // check for metform plugin is active or not. |
| 53 | if(in_array('metform/metform.php', apply_filters('active_plugins', get_option('active_plugins'))) && is_admin()) { |
| 54 | add_action('current_screen', [$this, 'redirect_to_metform_page']); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get forms dir. |
| 60 | * |
| 61 | * @since 2.6.3 |
| 62 | * @access public |
| 63 | */ |
| 64 | public static function get_dir() { |
| 65 | |
| 66 | return \ElementsKit_Lite::lib_dir() . 'forms/'; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get forms dir url. |
| 71 | * |
| 72 | * @since 2.6.3 |
| 73 | * @access public |
| 74 | */ |
| 75 | public static function get_url() { |
| 76 | |
| 77 | return \ElementsKit_Lite::lib_url() . 'forms/'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Enqueuing js/ css |
| 82 | * |
| 83 | * @since 2.6.3 |
| 84 | * @access public |
| 85 | */ |
| 86 | public function enqueue_scripts() { |
| 87 | |
| 88 | $current_screen = get_current_screen(); |
| 89 | |
| 90 | if(!empty($current_screen->id) && $current_screen->id === 'elementskit_page_forms') { |
| 91 | wp_enqueue_style( 'elementskit-forms', self::get_url() . 'assets/css/forms.css', [], \ElementsKit_Lite::version() ); |
| 92 | wp_enqueue_script('elementskit-forms', self::get_url() . 'assets/js/forms.js', ['jquery'], \ElementsKit_Lite::version(), true); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds a submenu page under elementskit menu. |
| 98 | * |
| 99 | * @since 2.6.3 |
| 100 | * @access public |
| 101 | */ |
| 102 | public function register_sub_menu() { |
| 103 | |
| 104 | add_submenu_page( |
| 105 | 'elementskit', |
| 106 | esc_html__('Forms', 'elementskit-lite'), |
| 107 | esc_html__('Forms', 'elementskit-lite'), |
| 108 | 'manage_options', |
| 109 | 'forms', |
| 110 | [$this, 'form_page_callback'] |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Display callback for the form submenu page. |
| 116 | * |
| 117 | * @since 2.6.3 |
| 118 | * @access public |
| 119 | */ |
| 120 | public function form_page_callback() { |
| 121 | |
| 122 | // Include template file |
| 123 | include self::get_dir() . 'pages/template.php'; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Redirect to metfor plugin page |
| 128 | * |
| 129 | * @since 2.6.3 |
| 130 | * @access public |
| 131 | */ |
| 132 | public function redirect_to_metform_page() { |
| 133 | |
| 134 | $current_screen = get_current_screen(); |
| 135 | |
| 136 | if(!empty($current_screen->id) && $current_screen->id === 'elementskit_page_forms') { |
| 137 | wp_safe_redirect(admin_url('edit.php?post_type=metform-form')); |
| 138 | exit; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | endif; |
| 144 |