| 1 |
<?php |
| 2 |
/** |
| 3 |
* New Item Page Class |
| 4 |
* |
| 5 |
* @package Woody_Code_Snippets |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* WINP_NewItem Class |
| 15 |
*/ |
| 16 |
class WINP_NewItem { |
| 17 |
|
| 18 |
/** |
| 19 |
* Singleton instance |
| 20 |
* |
| 21 |
* @var WINP_NewItem|null |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Private constructor to prevent direct instantiation |
| 27 |
*/ |
| 28 |
private function __construct() { |
| 29 |
add_action( 'admin_menu', [ $this, 'register_new_item_page' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register new item page |
| 34 |
*/ |
| 35 |
public function register_new_item_page(): void { |
| 36 |
$page_hook_suffix = add_submenu_page( |
| 37 |
'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE, |
| 38 |
'+ ' . __( 'Add Snippet', 'insert-php' ), |
| 39 |
'+ ' . __( 'Add Snippet', 'insert-php' ), |
| 40 |
WINP_Helper::has_post_capabilities(), |
| 41 |
'winp-new-item', |
| 42 |
[ $this, 'render_new_item_page' ], |
| 43 |
1 |
| 44 |
); |
| 45 |
|
| 46 |
add_action( "admin_print_scripts-$page_hook_suffix", [ $this, 'enqueue_new_item_assets' ] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Render new item page content |
| 51 |
*/ |
| 52 |
public function render_new_item_page(): void { |
| 53 |
echo '<div id="woody-new-item"></div>'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Load assets for new item page |
| 58 |
*/ |
| 59 |
public function enqueue_new_item_assets(): void { |
| 60 |
$asset_file = include WINP_PLUGIN_DIR . '/admin/assets/new-item/build/index.asset.php'; |
| 61 |
|
| 62 |
wp_enqueue_style( |
| 63 |
'winp-new-item-styles', |
| 64 |
WINP_PLUGIN_URL . '/admin/assets/new-item/build/style-index.css', |
| 65 |
[], |
| 66 |
$asset_file['version'] |
| 67 |
); |
| 68 |
|
| 69 |
wp_enqueue_script( |
| 70 |
'winp-new-item-scripts', |
| 71 |
WINP_PLUGIN_URL . '/admin/assets/new-item/build/index.js', |
| 72 |
$asset_file['dependencies'], |
| 73 |
$asset_file['version'], |
| 74 |
true |
| 75 |
); |
| 76 |
|
| 77 |
wp_set_script_translations( 'winp-new-item-scripts', 'insert-php' ); |
| 78 |
|
| 79 |
wp_localize_script( |
| 80 |
'winp-new-item-scripts', |
| 81 |
'winpNewItemObjects', |
| 82 |
[ |
| 83 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 84 |
'nonce' => wp_create_nonce( 'winp_new_item_nonce' ), |
| 85 |
'snippetTypes' => $this->get_snippet_types(), |
| 86 |
'postType' => WINP_SNIPPETS_POST_TYPE, |
| 87 |
'createUrl' => admin_url( 'post-new.php?post_type=' . WINP_SNIPPETS_POST_TYPE ), |
| 88 |
] |
| 89 |
); |
| 90 |
|
| 91 |
do_action( 'themeisle_internal_page', WINP_PLUGIN_SLUG, 'new-item' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get available snippet types |
| 96 |
* |
| 97 |
* @return array<array<string, string>> |
| 98 |
*/ |
| 99 |
private function get_snippet_types() { |
| 100 |
return [ |
| 101 |
[ |
| 102 |
'value' => 'php', |
| 103 |
'label' => __( 'PHP Snippet', 'insert-php' ), |
| 104 |
'description' => __( 'Execute PHP code on your site. Register functions, hooks, and global variables. Think of it as a virtual functions.php file.', 'insert-php' ), |
| 105 |
'icon' => 'dashicons-editor-code', |
| 106 |
], |
| 107 |
[ |
| 108 |
'value' => 'text', |
| 109 |
'label' => __( 'Text Snippet', 'insert-php' ), |
| 110 |
'description' => __( 'Insert formatted text content including quotes, paragraphs, tables, and media files. Supports shortcodes from other plugins.', 'insert-php' ), |
| 111 |
'icon' => 'dashicons-text', |
| 112 |
], |
| 113 |
[ |
| 114 |
'value' => 'universal', |
| 115 |
'label' => __( 'Universal Snippet', 'insert-php' ), |
| 116 |
'description' => __( 'Insert any combination of PHP, HTML, JavaScript, and CSS code. Perfect for ads, analytics, embeds, and complex scenarios.', 'insert-php' ), |
| 117 |
'icon' => 'dashicons-admin-site', |
| 118 |
], |
| 119 |
[ |
| 120 |
'value' => 'css', |
| 121 |
'label' => __( 'CSS Snippet', 'insert-php' ), |
| 122 |
'description' => __( 'Add custom CSS styles to your site. Modify appearance and layout without editing theme files.', 'insert-php' ), |
| 123 |
'icon' => 'dashicons-art', |
| 124 |
], |
| 125 |
[ |
| 126 |
'value' => 'js', |
| 127 |
'label' => __( 'JavaScript Snippet', 'insert-php' ), |
| 128 |
'description' => __( 'Add custom JavaScript to your site. Ideal for analytics, interactive features, and third-party integrations.', 'insert-php' ), |
| 129 |
'icon' => 'dashicons-media-code', |
| 130 |
], |
| 131 |
[ |
| 132 |
'value' => 'html', |
| 133 |
'label' => __( 'HTML Snippet', 'insert-php' ), |
| 134 |
'description' => __( 'Insert custom HTML markup anywhere on your site. Add structured content and layout elements.', 'insert-php' ), |
| 135 |
'icon' => 'dashicons-editor-table', |
| 136 |
], |
| 137 |
[ |
| 138 |
'value' => 'ad', |
| 139 |
'label' => __( 'Ad Snippet', 'insert-php' ), |
| 140 |
'description' => __( 'Insert advertisement code and banners. Manage ad placements with ease across your site.', 'insert-php' ), |
| 141 |
'icon' => 'dashicons-megaphone', |
| 142 |
], |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Prevent cloning of the instance |
| 148 |
* |
| 149 |
* @throws \Exception An exception when trying to clone the instance. |
| 150 |
*/ |
| 151 |
private function __clone() { |
| 152 |
throw new \Exception( 'Cannot clone singleton' ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Prevent unserialization of the instance |
| 157 |
* |
| 158 |
* @throws \Exception An exception when trying to unserialize the instance. |
| 159 |
*/ |
| 160 |
public function __wakeup() { |
| 161 |
throw new \Exception( 'Cannot unserialize singleton' ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get singleton instance |
| 166 |
* |
| 167 |
* @return WINP_NewItem |
| 168 |
*/ |
| 169 |
public static function get_instance() { |
| 170 |
if ( null === self::$instance ) { |
| 171 |
self::$instance = new self(); |
| 172 |
} |
| 173 |
return self::$instance; |
| 174 |
} |
| 175 |
} |
| 176 |
|