| 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 |
|
| 77 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Just checking tab parameter for UI display |
| 78 |
if ( isset( $_GET['tab'] ) && 'library' === $_GET['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 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Just checking tab parameter for display logic |
| 122 |
$is_library_tab = isset( $_GET['tab'] ) && 'library' === $_GET['tab']; |
| 123 |
|
| 124 |
if ( $is_pro || $is_library_tab ) : |
| 125 |
?> |
| 126 |
<!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions --> |
| 127 |
<form id="winp-snippet-library" method="get"> |
| 128 |
<!-- For plugins, we also need to ensure that the form posts back to our current page --> |
| 129 |
<input type="hidden" name="page" value="<?php echo esc_attr( isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '' ); ?>"/> |
| 130 |
<!-- Now we can render the completed list table --> |
| 131 |
<?php |
| 132 |
$snippet_list_table->prepare_items( $common ); |
| 133 |
$snippet_list_table->display(); |
| 134 |
?> |
| 135 |
</form> |
| 136 |
|
| 137 |
<?php wp_nonce_field( 'winp-snippet-library', 'winp-snippet-library-nonce' ); ?> |
| 138 |
<?php else : ?> |
| 139 |
<div class="winp-upsell-container"> |
| 140 |
<div class="winp-upsell-card"> |
| 141 |
<div class="winp-upsell-icon"> |
| 142 |
<span class="dashicons dashicons-category"></span> |
| 143 |
</div> |
| 144 |
<div class="winp-upsell-title"> |
| 145 |
<?php esc_html_e( 'My Templates', 'insert-php' ); ?> |
| 146 |
</div> |
| 147 |
<p class="winp-upsell-badge"> |
| 148 |
<?php esc_html_e( 'Pro feature', 'insert-php' ); ?> |
| 149 |
</p> |
| 150 |
<p class="winp-upsell-description"> |
| 151 |
<?php esc_html_e( 'Snippets saved as templates sync automatically across all your sites—no import/export needed.', 'insert-php' ); ?> |
| 152 |
</p> |
| 153 |
<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"> |
| 154 |
<?php esc_html_e( 'Upgrade to Pro', 'insert-php' ); ?> |
| 155 |
</a> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
<?php |
| 159 |
endif; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Prevent cloning of the instance |
| 164 |
* |
| 165 |
* @throws \Exception An exception when trying to clone the instance. |
| 166 |
*/ |
| 167 |
private function __clone() { |
| 168 |
throw new \Exception( 'Cannot clone singleton' ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Prevent unserialization of the instance |
| 173 |
* |
| 174 |
* @throws \Exception An exception when trying to unserialize the instance. |
| 175 |
*/ |
| 176 |
public function __wakeup() { |
| 177 |
throw new \Exception( 'Cannot unserialize singleton' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get singleton instance |
| 182 |
* |
| 183 |
* @return WINP_SnippetLibrary |
| 184 |
*/ |
| 185 |
public static function get_instance() { |
| 186 |
if ( null === self::$instance ) { |
| 187 |
self::$instance = new self(); |
| 188 |
} |
| 189 |
return self::$instance; |
| 190 |
} |
| 191 |
} |
| 192 |
|