/** * Executed when the menu is loaded */ public function load() { parent::load(); /* Don't allow visiting the edit snippet page without a valid ID */ if (code_snippets_get_menu_slug('edit') === $_REQUEST['page']) { if (!isset($_REQUEST['id']) || 0 == $_REQUEST['id']) { wp_redirect(code_snippets_get_menu_url('add')); exit; } } /* Load the contextual help tabs */ code_snippets_load_edit_help(); /* Enqueue the code editor and other scripts and styles */ add_action('admin_enqueue_scripts', 'code_snippets_enqueue_codemirror'); add_action('admin_enqueue_scripts', array($this, 'enqueue_tagit'), 9); /* Register action hooks */ if (code_snippets_get_setting('general', 'enable_description')) { add_action('code_snippets/admin/single', array($this, 'render_description_editor'), 9); } if (code_snippets_get_setting('general', 'enable_tags')) { add_action('code_snippets/admin/single', array($this, 'render_tags_editor')); } if (code_snippets_get_setting('general', 'snippet_scope_enabled')) { add_action('code_snippets/admin/single/settings', array($this, 'render_scope_setting')); } if (get_current_screen()->in_admin('network')) { add_action('code_snippets/admin/single/settings', array($this, 'render_multisite_sharing_setting')); } $this->process_actions(); }
/** * Registers and loads the CodeMirror library * * @uses wp_enqueue_style() to add the stylesheets to the queue * @uses wp_enqueue_script() to add the scripts to the queue */ function code_snippets_enqueue_codemirror() { $codemirror_version = '5.10.0'; $url = plugin_dir_url(CODE_SNIPPETS_FILE); /* Remove other CodeMirror styles */ wp_deregister_style('codemirror'); wp_deregister_style('wpeditor'); /* CodeMirror */ wp_enqueue_style('code-snippets-codemirror', $url . 'css/min/codemirror.css', false, $codemirror_version); wp_enqueue_script('code-snippets-codemirror', $url . 'js/min/codemirror.js', false, $codemirror_version); /* CodeMirror Theme */ $theme = code_snippets_get_setting('editor', 'theme'); if ('default' !== $theme) { wp_enqueue_style('code-snippets-codemirror-theme-' . $theme, $url . "css/min/cmthemes/{$theme}.css", array('code-snippets-codemirror'), $codemirror_version); } }
/** * Render a number select field for an editor setting * @param array $atts The setting field's attributes */ function code_snippets_number_field($atts) { printf('<input type="number" name="code_snippets_settings[%s][%s]" value="%s"', $atts['section'], $atts['id'], code_snippets_get_setting($atts['section'], $atts['id'])); if (isset($atts['min'])) { printf(' min="%d"', $atts['min']); } if (isset($atts['max'])) { printf(' max="%d"', $atts['max']); } echo '>'; if (!empty($atts['label'])) { echo ' ' . $atts['label']; } // Add field description if it is set if (!empty($atts['desc'])) { echo '<p class="description">' . $atts['desc'] . '</p>'; } }
function enqueue_prism($posts) { if (empty($posts) || code_snippets_get_setting('general', 'disable_prism')) { return $posts; } $found = false; foreach ($posts as $post) { if (false !== stripos($post->post_content, '[code_snippet')) { $found = true; break; } } if (!$found) { return $posts; } $plugin = code_snippets(); wp_enqueue_style('code-snippets-prism', plugins_url('js/vendor/prism.css', $plugin->file), array(), $plugin->version); wp_enqueue_script('code-snippets-prism', plugins_url('js/vendor/prism.js', $plugin->file), array(), $plugin->version, true); return $posts; }
/** * Render a theme select field */ function code_snippets_codemirror_theme_select_field($atts) { $saved_value = code_snippets_get_setting($atts['section'], $atts['id']); echo '<select name="code_snippets_settings[editor][theme]">'; echo '<option value="default"' . selected('default', $saved_value, false) . '>Default</option>'; /* Fetch all theme CSS files */ $themes_dir = plugin_dir_path(CODE_SNIPPETS_FILE) . 'css/min/cmthemes/'; $themes = glob($themes_dir . '*.css'); /* Print dropdown entry for each theme */ foreach ($themes as $theme) { /* Extract theme name from path */ $theme = str_replace($themes_dir, '', $theme); $theme = str_replace('.css', '', $theme); /* Skip mobile themes */ if ('ambiance-mobile' === $theme) { continue; } printf('<option value="%s"%s>%s</option>', $theme, selected($theme, $saved_value, false), ucwords(str_replace('-', ' ', $theme))); } echo '</select>'; }
?> </textarea> <?php /* Allow plugins to add fields and content to this page */ do_action('code_snippets/admin/single', $snippet); /* Add a nonce for security */ wp_nonce_field('save_snippet'); ?> <p class="submit"> <?php /* Make the 'Save and Activate' button the default if the setting is enabled */ if ($snippet->shared_network && get_current_screen()->in_admin('network')) { submit_button(null, 'primary', 'save_snippet', false); } elseif (!$snippet->active && code_snippets_get_setting('general', 'activate_by_default')) { submit_button(__('Save Changes and Activate', 'code-snippets'), 'primary', 'save_snippet_activate', false); submit_button(null, 'secondary', 'save_snippet', false); } else { /* Save Snippet button */ submit_button(null, 'primary', 'save_snippet', false); /* Save Snippet and Activate/Deactivate button */ if (!$snippet->active) { submit_button(__('Save Changes and Activate', 'code-snippets'), 'secondary', 'save_snippet_activate', false); } else { submit_button(__('Save Changes and Deactivate', 'code-snippets'), 'secondary', 'save_snippet_deactivate', false); } } if (0 !== $snippet->id) { /* Export button */ submit_button(__('Export', 'code-snippets'), 'secondary', 'export_snippet', false);
/** * Prepares the items to later display in the table. * Should run before any headers are sent. */ function prepare_items() { global $status, $snippets, $totals, $page, $orderby, $order, $s; wp_reset_vars(array('orderby', 'order', 's')); $screen = get_current_screen(); $user = get_current_user_id(); /* First, lets process the bulk actions */ $this->process_bulk_actions(); $snippets = array('all' => apply_filters('code_snippets/list_table/get_snippets', get_snippets($screen->is_network)), 'active' => array(), 'inactive' => array(), 'recently_activated' => array(), 'admin' => array(), 'frontend' => array()); /* Filter snippets by tag */ if (isset($_POST['tag'])) { $location = empty($_POST['tag']) ? remove_query_arg('tag') : add_query_arg('tag', $_POST['tag']); wp_redirect(esc_url_raw($location)); } if (!empty($_GET['tag'])) { $snippets['all'] = array_filter($snippets['all'], array($this, '_tags_filter_callback')); } /* Filter snippets based on search query */ if ($s) { $snippets['all'] = array_filter($snippets['all'], array($this, '_search_callback')); } if ($screen->is_network) { $recently_activated = get_site_option('recently_activated_snippets', array()); } else { $recently_activated = get_option('recently_activated_snippets', array()); } $one_week = 7 * 24 * 60 * 60; foreach ($recently_activated as $key => $time) { if ($time + $one_week < time()) { unset($recently_activated[$key]); } } if ($screen->is_network) { update_site_option('recently_activated_snippets', $recently_activated); } else { update_option('recently_activated_snippets', $recently_activated); } $scopes_enabled = code_snippets_get_setting('general', 'snippet_scope_enabled'); foreach ((array) $snippets['all'] as $snippet) { /* Filter into individual sections */ if ($snippet->active) { $snippets['active'][] = $snippet; } else { // Was the snippet recently activated? if (isset($recently_activated[$snippet->id])) { $snippets['recently_activated'][] = $snippet; } $snippets['inactive'][] = $snippet; } if ($scopes_enabled) { if ('1' == $snippet->scope) { $snippets['admin'][] = $snippet; } elseif ('2' == $snippet->scope) { $snippets['frontend'][] = $snippet; } } } $totals = array(); foreach ($snippets as $type => $list) { $totals[$type] = count($list); } if (empty($snippets[$status])) { $status = 'all'; } $data = $snippets[$status]; /* * First, lets decide how many records per page to show * by getting the user's setting in the Screen Options * panel. */ $sort_by = $screen->get_option('per_page', 'option'); $screen_option = $screen->get_option('per_page', 'option'); $per_page = get_user_meta($user, $screen_option, true); if (empty($per_page) || $per_page < 1) { $per_page = $screen->get_option('per_page', 'default'); } $per_page = (int) $per_page; $this->_column_headers = $this->get_column_info(); usort($data, array($this, 'usort_reorder_callback')); /* * Let's figure out what page the user is currently * looking at. */ $current_page = $this->get_pagenum(); /* * Let's check how many items are in our data array. */ $total_items = count($data); /* * The WP_List_Table class does not handle pagination for us, so we need * to ensure that the data is trimmed to only the current page. */ $data = array_slice($data, ($current_page - 1) * $per_page, $per_page); /* * Now we can add our *sorted* data to the items property, where * it can be used by the rest of the class. */ $this->items = $data; /* * We also have to register our pagination options & calculations. */ $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil($total_items / $per_page))); }
/** * Registers and loads the code editor's assets * * @since 1.7 * @access private * * @uses wp_enqueue_script() To add the scripts to the queue * @uses wp_enqueue_style() To add the stylesheets to the queue */ function code_snippets_enqueue_codemirror() { $tagit_version = '2.0'; $codemirror_version = '5.2'; $url = plugin_dir_url(CODE_SNIPPETS_FILE); /* Remove other CodeMirror styles */ wp_deregister_style('codemirror'); wp_deregister_style('wpeditor'); /* CodeMirror */ wp_enqueue_style('code-snippets-codemirror', $url . 'css/min/codemirror.css', false, $codemirror_version); wp_enqueue_script('code-snippets-codemirror', $url . 'js/min/codemirror.js', false, $codemirror_version); /* CodeMirror Theme */ $theme = code_snippets_get_setting('editor', 'theme'); if ('default' !== $theme) { wp_enqueue_style('code-snippets-codemirror-theme-' . $theme, $url . "css/min/cmthemes/{$theme}.css", array('code-snippets-codemirror'), $codemirror_version); } /* Tag It UI */ wp_enqueue_script('code-snippets-tag-it', $url . 'js/min/tag-it.js', array('jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position', 'jquery-ui-autocomplete', 'jquery-effects-blind', 'jquery-effects-highlight'), $tagit_version); wp_enqueue_style('code-snippets-tag-it', $url . 'css/min/tagit.css', false, $tagit_version); }
/** * Outputs content for a single row of the table * * @param Snippet $snippet The snippet being used for the current row */ public function single_row($snippet) { $row_class = $snippet->active ? 'active' : 'inactive'; if (code_snippets_get_setting('general', 'snippet_scope_enabled')) { $row_class .= sprintf(' %s-scope', $snippet->scope_name); } if ($snippet->shared_network) { $row_class .= ' shared-network'; } printf('<tr class="%s">', $row_class); $this->single_row_columns($snippet); echo '</tr>'; }