function render_shortcode($atts) { $atts = shortcode_atts(array('id' => 0, 'network' => false), $atts, 'code_snippet'); if (!($id = intval($atts['id']))) { return ''; } $network = $atts['network'] ? true : false; $snippet = get_snippet($id, $network); if (!trim($snippet->code)) { return ''; } return '<pre><code class="language-php">' . esc_html($snippet->code) . '</code></pre>'; }
/** * Build the export file name * @return string */ public function get_filename() { if (1 == count($this->snippet_ids)) { /* If there is only snippet to export, use its name instead of the site name */ $snippet = get_snippet($this->snippet_ids[0], $this->table_name); $title = strtolower($snippet->name); } else { /* Otherwise, use the site name as set in Settings > General */ $title = strtolower(get_bloginfo('name')); } $filename = "{$title}.code-snippets.{$this->format}"; $filename = apply_filters('code_snippets/export/filename', $filename, $title); return sanitize_file_name($filename); }
/** * Retrieve a list of snippets from the database * * @since 2.0 * * @uses $wpdb to query the database for snippets * @uses get_snippets_table_name() to dynamically retrieve the snippet table name * * @param array $ids The IDs of the snippets to fetch * @param bool|null $multisite Retrieve multisite-wide or site-wide snippets? * @return array An array of Snippet objects */ function get_snippets(array $ids = array(), $multisite = null) { /** @var wpdb $wpdb */ global $wpdb; $table = get_snippets_table_name($multisite); $sql = "SELECT * FROM {$table}"; $ids_count = count($ids); if (1 == $ids_count) { return get_snippet($ids[0]); } if ($ids_count > 1) { $sql .= ' WHERE id IN ('; $sql .= implode(',', array_fill(0, $ids_count, '%d')); $sql .= ')'; $sql = $wpdb->prepare($sql, $ids); } $snippets = $wpdb->get_results($sql, ARRAY_A); /* Convert snippets to snippet objects */ foreach ($snippets as $index => $snippet) { $snippet['network'] = $multisite; $snippets[$index] = new Snippet($snippet); } return apply_filters('code_snippets/get_snippets', $snippets, $multisite); }
<?php /** * HTML code for the Add New/Edit Snippet page * * @package Code_Snippets * @subpackage Views */ /* Bail if accessed directly */ if (!defined('ABSPATH')) { return; } global $pagenow; $table = get_snippets_table_name(); $edit_id = code_snippets_get_menu_slug('edit') === $_REQUEST['page'] ? absint($_REQUEST['id']) : 0; $snippet = get_snippet($edit_id); ?> <div class="wrap"> <h1><?php if ($edit_id) { esc_html_e('Edit Snippet', 'code-snippets'); printf(' <a href="%1$s" class="page-title-action add-new-h2">%2$s</a>', code_snippets_get_menu_url('add'), esc_html_x('Add New', 'snippet', 'code-snippets')); } else { esc_html_e('Add New Snippet', 'code-snippets'); } ?> </h1> <form method="post" action="" style="margin-top: 10px;"> <?php /* Output the hidden fields */
/** * Retrieve the first error in a snippet's code * * @param $snippet_id * * @return array|bool */ private function get_snippet_error($snippet_id) { if (!intval($snippet_id)) { return false; } $snippet = get_snippet(intval($snippet_id)); if ('' === $snippet->code) { return false; } @eval($snippet->code); $error = error_get_last(); if (is_null($error)) { return false; } return $error; }
function get_snippet($file, $data = array()) { return get_snippet($file, $data); }
/** * Retrieve the first error in a snippet's code * * @param $snippet_id * * @return array|bool */ private function get_snippet_error($snippet_id) { if (!intval($snippet_id)) { return false; } $snippet = get_snippet(intval($snippet_id)); if ('' === $snippet->code) { return false; } ob_start(); $result = eval($snippet->code); ob_end_clean(); if (false !== $result) { return false; } $error = error_get_last(); if (is_null($error)) { return false; } return $error; }