PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.7
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
insert-php / admin / pages / class.snippet-library.php

class.snippet-library.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.7.7, at admin/pages/class.snippet-library.php

191 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Snippet Library 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_SnippetLibrary Class
15 */
16 class WINP_SnippetLibrary {
17
18 /**
19 * Singleton instance
20 *
21 * @var WINP_SnippetLibrary|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_snippet_library_page' ] );
30
31 require_once WINP_PLUGIN_DIR . '/admin/includes/class.snippets.table.php';
32 }
33
34 /**
35 * Register snippet library page
36 */
37 public function register_snippet_library_page(): void {
38 $page_hook_suffix = add_submenu_page(
39 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE,
40 __( 'Snippets Library', 'insert-php' ),
41 __( 'Snippets Library', 'insert-php' ),
42 WINP_Helper::has_post_capabilities(),
43 'snippet-library',
44 [ $this, 'render_snippet_library_page' ]
45 );
46
47 add_action( "admin_print_scripts-$page_hook_suffix", [ $this, 'enqueue_snippet_library_assets' ] );
48 }
49
50 /**
51 * Load assets for snippet library page
52 */
53 public function enqueue_snippet_library_assets(): void {
54 wp_enqueue_style( 'winp-snippets-table', WINP_PLUGIN_URL . '/admin/assets/css/snippets-table.css', [], WINP_PLUGIN_VERSION );
55 wp_enqueue_script( 'winp-snippet-library', WINP_PLUGIN_URL . '/admin/assets/js/snippet-library.js', [ 'jquery' ], WINP_PLUGIN_VERSION, true );
56 wp_localize_script(
57 'winp-snippet-library',
58 'winp_snippet_library',
59 [
60 'is_import' => __( 'Import snippet?', 'insert-php' ),
61 'is_delete' => __( 'Delete snippet?', 'insert-php' ),
62 'import_failed' => __( 'Import failed. Please check your file format and try again.', 'insert-php' ),
63 'delete_failed' => __( 'Delete failed. Please refresh the page and try again.', 'insert-php' ),
64 ]
65 );
66 }
67
68 /**
69 * Render snippet library page content
70 */
71 public function render_snippet_library_page(): void {
72 $my_snippets_tab = true;
73 $library_tab = false;
74 $my_snippets_url = esc_url_raw( remove_query_arg( [ 'tab' ] ) );
75 $library_url = esc_url_raw( add_query_arg( 'tab', 'library', $my_snippets_url ) );
76 $current_tab = WINP_HTTP::get( 'tab', '', 'sanitize_key' );
77
78 if ( 'library' === $current_tab ) {
79 $my_snippets_tab = false;
80 $library_tab = true;
81 }
82
83 do_action( 'themeisle_internal_page', WINP_PLUGIN_SLUG, 'snippet-library' );
84 ?>
85 <div class="wrap">
86 <div class="winp-snippet-library">
87 <h3><?php esc_html_e( 'Snippets Library', 'insert-php' ); ?></h3>
88
89 <div class="nav-tab-wrapper">
90 <a href="<?php echo esc_url( $my_snippets_url ); ?>" class="nav-tab<?php echo $my_snippets_tab ? ' nav-tab-active' : ''; ?>">
91 <?php esc_html_e( 'My Snippets', 'insert-php' ); ?>
92 </a>
93 <a href="<?php echo esc_url( $library_url ); ?>" class="nav-tab<?php echo $library_tab ? ' nav-tab-active' : ''; ?>">
94 <?php esc_html_e( 'Snippets Library', 'insert-php' ); ?>
95 </a>
96 </div>
97
98 <?php if ( $library_tab ) : ?>
99 <div id="tab1">
100 <?php $this->render_html( true ); ?>
101 </div>
102 <?php else : ?>
103 <div id="tab2">
104 <?php $this->render_html( false ); ?>
105 </div>
106 <?php endif; ?>
107 </div>
108 </div>
109 <?php
110 }
111
112 /**
113 * Render html part with snippets list
114 *
115 * @param bool $common Whether to show common snippets (library) or user snippets.
116 */
117 private function render_html( bool $common ): void {
118 $snippet_list_table = new WINP_Snippet_Library_Table();
119
120 $is_pro = WINP_Plugin::app()->get_api_object()->is_key();
121 $is_library_tab = 'library' === WINP_HTTP::get( 'tab', '', 'sanitize_key' );
122
123 if ( $is_pro || $is_library_tab ) :
124 ?>
125 <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
126 <form id="winp-snippet-library" method="get">
127 <!-- For plugins, we also need to ensure that the form posts back to our current page -->
128 <input type="hidden" name="page" value="<?php echo esc_attr( isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '' ); ?>"/>
129 <!-- Now we can render the completed list table -->
130 <?php
131 $snippet_list_table->prepare_items( $common );
132 $snippet_list_table->display();
133 ?>
134 </form>
135
136 <?php wp_nonce_field( 'winp-snippet-library', 'winp-snippet-library-nonce' ); ?>
137 <?php else : ?>
138 <div class="winp-upsell-container">
139 <div class="winp-upsell-card">
140 <div class="winp-upsell-icon">
141 <span class="dashicons dashicons-category"></span>
142 </div>
143 <div class="winp-upsell-title">
144 <?php esc_html_e( 'My Templates', 'insert-php' ); ?>
145 </div>
146 <p class="winp-upsell-badge">
147 <?php esc_html_e( 'Pro feature', 'insert-php' ); ?>
148 </p>
149 <p class="winp-upsell-description">
150 <?php esc_html_e( 'Snippets saved as templates sync automatically across all your sites—no import/export needed.', 'insert-php' ); ?>
151 </p>
152 <a href="<?php echo esc_url( tsdk_utmify( WINP_UPGRADE, 'snippet_library', 'my_templates_upsell' ) ); ?>" class="button button-primary button-large winp-upsell-button" target="_blank">
153 <?php esc_html_e( 'Upgrade to Pro', 'insert-php' ); ?>
154 </a>
155 </div>
156 </div>
157 <?php
158 endif;
159 }
160
161 /**
162 * Prevent cloning of the instance
163 *
164 * @throws \Exception An exception when trying to clone the instance.
165 */
166 private function __clone() {
167 throw new \Exception( 'Cannot clone singleton' );
168 }
169
170 /**
171 * Prevent unserialization of the instance
172 *
173 * @throws \Exception An exception when trying to unserialize the instance.
174 */
175 public function __wakeup() {
176 throw new \Exception( 'Cannot unserialize singleton' );
177 }
178
179 /**
180 * Get singleton instance
181 *
182 * @return WINP_SnippetLibrary
183 */
184 public static function get_instance() {
185 if ( null === self::$instance ) {
186 self::$instance = new self();
187 }
188 return self::$instance;
189 }
190 }
191