PluginProbe
Code Snippets / 2.13.3
Code Snippets v2.13.3
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / admin-menus / class-manage-menu.php

class-manage-menu.php in Code Snippets 2.13.3, at php/admin-menus/class-manage-menu.php

242 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This class handles the manage snippets menu
5 * @since 2.4.0
6 * @package Code_Snippets
7 */
8 class Code_Snippets_Manage_Menu extends Code_Snippets_Admin_Menu {
9
10 /**
11 * Holds the list table class
12 * @var Code_Snippets_List_Table
13 */
14 public $list_table;
15
16 /**
17 * Class constructor
18 */
19 public function __construct() {
20
21 parent::__construct( 'manage',
22 _x( 'All Snippets', 'menu label', 'code-snippets' ),
23 __( 'Snippets', 'code-snippets' )
24 );
25 }
26
27 /**
28 * Register action and filter hooks
29 */
30 public function run() {
31 parent::run();
32
33 if ( code_snippets()->admin->is_compact_menu() ) {
34 add_action( 'admin_menu', array( $this, 'register_compact_menu' ), 2 );
35 add_action( 'network_admin_menu', array( $this, 'register_compact_menu' ), 2 );
36 }
37
38 add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 );
39 add_action( 'wp_ajax_update_code_snippet', array( $this, 'ajax_callback' ) );
40 }
41
42 /**
43 * Register the top-level 'Snippets' menu and associated 'Manage' subpage
44 *
45 * @uses add_menu_page() to register a top-level menu
46 * @uses add_submenu_page() to register a sub-menu
47 */
48 function register() {
49
50 /* Register the top-level menu */
51 add_menu_page(
52 __( 'Snippets', 'code-snippets' ),
53 _x( 'Snippets', 'top-level menu label', 'code-snippets' ),
54 code_snippets()->get_cap(),
55 code_snippets()->get_menu_slug(),
56 array( $this, 'render' ),
57 'div', // icon is added through CSS
58 is_network_admin() ? 21 : 67
59 );
60
61 /* Register the sub-menu */
62 parent::register();
63 }
64
65 public function register_compact_menu() {
66
67 if ( ! code_snippets()->admin->is_compact_menu() ) {
68 return;
69 }
70
71 $sub = code_snippets()->get_menu_slug( isset( $_GET['sub'] ) ? $_GET['sub'] : 'snippets' );
72
73 $classmap = array(
74 'snippets' => 'manage',
75 'add-snippet' => 'edit',
76 'edit-snippet' => 'edit',
77 'import-snippets' => 'import',
78 'snippets-settings' => 'settings',
79 );
80
81 if ( isset( $classmap[ $sub ], code_snippets()->admin->menus[ $classmap[ $sub ] ] ) ) {
82 /** @var Code_Snippets_Admin_Menu $class */
83 $class = code_snippets()->admin->menus[ $classmap[ $sub ] ];
84 } else {
85 $class = $this;
86 }
87
88 /* Add a submenu to the Tools menu */
89 $hook = add_submenu_page(
90 'tools.php',
91 __( 'Snippets', 'code-snippets' ),
92 _x( 'Snippets', 'tools submenu label', 'code-snippets' ),
93 code_snippets()->get_cap(),
94 code_snippets()->get_menu_slug(),
95 array( $class, 'render' )
96 );
97
98 add_action( 'load-' . $hook, array( $class, 'load' ) );
99
100 }
101
102 /**
103 * Executed when the admin page is loaded
104 */
105 function load() {
106 parent::load();
107
108 /* Load the contextual help tabs */
109 $contextual_help = new Code_Snippets_Contextual_Help( 'manage' );
110 $contextual_help->load();
111
112 /* Initialize the list table class */
113 $this->list_table = new Code_Snippets_List_Table();
114 $this->list_table->prepare_items();
115 }
116
117 /**
118 * Enqueue scripts and stylesheets for the admin page
119 */
120 public function enqueue_assets() {
121 $plugin = code_snippets();
122 $rtl = is_rtl() ? '-rtl' : '';
123
124 wp_enqueue_style(
125 'code-snippets-manage',
126 plugins_url( "css/min/manage{$rtl}.css", $plugin->file ),
127 array(), $plugin->version
128 );
129
130 wp_enqueue_script(
131 'code-snippets-manage-js',
132 plugins_url( 'js/min/manage.js', $plugin->file ),
133 array(), $plugin->version, true
134 );
135 }
136
137 /**
138 * Print the status and error messages
139 */
140 protected function print_messages() {
141
142 /* Output a warning if safe mode is active */
143 if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
144 echo '<div id="message" class="error fade"><p>';
145 _e( '<strong>Warning:</strong> Safe mode is active and snippets will not execute! Remove the <code>CODE_SNIPPETS_SAFE_MODE</code> constant from <code>wp-config.php</code> to turn off safe mode. <a href="https://github.com/sheabunge/code-snippets/wiki/Safe-Mode" target="_blank">Help</a>', 'code-snippets' );
146 echo '</p></div>';
147 }
148
149 echo $this->get_result_message(
150 array(
151 'executed' => __( 'Snippet <strong>executed</strong>.', 'code-snippets' ),
152 'activated' => __( 'Snippet <strong>activated</strong>.', 'code-snippets' ),
153 'activated-multi' => __( 'Selected snippets <strong>activated</strong>.', 'code-snippets' ),
154 'deactivated' => __( 'Snippet <strong>deactivated</strong>.', 'code-snippets' ),
155 'deactivated-multi' => __( 'Selected snippets <strong>deactivated</strong>.', 'code-snippets' ),
156 'deleted' => __( 'Snippet <strong>deleted</strong>.', 'code-snippets' ),
157 'deleted-multi' => __( 'Selected snippets <strong>deleted</strong>.', 'code-snippets' ),
158 'cloned' => __( 'Snippet <strong>cloned</strong>.', 'code-snippets' ),
159 'cloned-multi' => __( 'Selected snippets <strong>cloned</strong>.', 'code-snippets' ),
160 )
161 );
162 }
163
164 /**
165 * Handles saving the user's snippets per page preference
166 *
167 * @param mixed $status
168 * @param string $option The screen option name
169 * @param mixed $value
170 *
171 * @return mixed
172 */
173 function save_screen_option( $status, $option, $value ) {
174 if ( 'snippets_per_page' === $option ) {
175 return $value;
176 }
177
178 return $status;
179 }
180
181 /**
182 * Handle AJAX requests
183 */
184 public function ajax_callback() {
185 check_ajax_referer( 'code_snippets_manage' );
186
187 if ( ! isset( $_POST['field'], $_POST['snippet'] ) ) {
188 wp_die( 'Snippet data not provided' );
189 }
190
191 $snippet_data = json_decode( stripslashes( $_POST['snippet'] ), true );
192
193 $snippet = new Code_Snippet( $snippet_data );
194 $field = $_POST['field'];
195
196 if ( 'priority' === $field ) {
197
198 if ( ! isset( $snippet_data['priority'] ) || ! is_numeric( $snippet_data['priority'] ) ) {
199 wp_die( 'missing snippet priority data' );
200 }
201
202 global $wpdb;
203
204 $wpdb->update(
205 code_snippets()->db->get_table_name( $snippet->network ),
206 array( 'priority' => $snippet->priority ),
207 array( 'id' => $snippet->id ),
208 array( '%d' ),
209 array( '%d' )
210 );
211
212 } elseif ( 'active' === $field ) {
213
214 if ( ! isset( $snippet_data['active'] ) ) {
215 wp_die( 'missing snippet active data' );
216 }
217
218 if ( $snippet->shared_network ) {
219 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
220
221 if ( in_array( $snippet->id, $active_shared_snippets ) !== $snippet->active ) {
222
223 $active_shared_snippets = $snippet->active ?
224 array_merge( $active_shared_snippets, array( $snippet->id ) ) :
225 array_diff( $active_shared_snippets, array( $snippet->id ) );
226
227 update_option( 'active_shared_network_snippets', $active_shared_snippets );
228 }
229 } else {
230
231 if ( $snippet->active ) {
232 activate_snippet( $snippet->id, $snippet->network );
233 } else {
234 deactivate_snippet( $snippet->id, $snippet->network );
235 }
236 }
237 }
238
239 wp_die();
240 }
241 }
242