public static function init_settings() { if (!self::$settings) { // Add settings CrayonSettingsWP::load_settings(TRUE); self::$settings = array('url' => plugins_url(CRAYON_TE_CONTENT_PHP, __FILE__), 'home_url' => home_url(), 'css' => 'crayon-te', 'used' => CrayonGlobalSettings::val(CrayonSettings::TINYMCE_USED), 'used_setting' => CrayonSettings::TINYMCE_USED, 'ajax_url' => plugins_url(CRAYON_AJAX_PHP, dirname(dirname(__FILE__))), 'css_selected' => 'crayon-selected', 'code_css' => '#crayon-code', 'url_css' => '#crayon-url', 'url_info_css' => '#crayon-te-url-info', 'lang_css' => '#crayon-lang', 'title_css' => '#crayon-title', 'mark_css' => '#crayon-mark', 'inline_css' => 'crayon-inline', 'inline_hide_css' => 'crayon-hide-inline', 'inline_hide_only_css' => 'crayon-hide-inline-only', 'hl_css' => '#crayon-highlight', 'switch_html' => '#content-html', 'switch_tmce' => '#content-tmce', 'tinymce_button' => '#content_crayon_tinymce', 'submit_css' => 'crayon-te-submit', 'submit_wrapper_css' => '#crayon-te-submit-wrapper', 'data_value' => 'data-value', 'attr_sep' => CrayonGlobalSettings::val_str(CrayonSettings::ATTR_SEP), 'css_sep' => '_', 'fallback_lang' => CrayonGlobalSettings::val(CrayonSettings::FALLBACK_LANG), 'dialog_title_add' => crayon__('Add Crayon Code'), 'dialog_title_edit' => crayon__('Edit Crayon Code'), 'submit_add' => crayon__('Add'), 'submit_edit' => crayon__('Save')); } }
public static function load_cache() { // Load cache from db if (!(self::$cache = get_option(self::CACHE))) { self::$cache = array(); update_option(self::CACHE, self::$cache); } }
/** * Converts Crayon tags found in WP to <pre> form. * XXX: This will alter blog content, so backup before calling. * XXX: Do NOT call this while updating posts or comments, it may cause an infinite loop or fail. * @param $encode Whether to detect missing "decode" attribute and encode html entities in the code. */ public static function convert_tags($encode = FALSE) { $crayon_posts = CrayonSettingsWP::load_legacy_posts(); if ($crayon_posts === NULL) { return; } self::init_legacy_tag_bits(); $args = array('callback' => 'CrayonWP::capture_replace_pre', 'callback_extra_args' => array('encode' => $encode), 'ignore' => FALSE, 'preserve_atts' => TRUE, 'flags' => self::$legacy_flags, 'skip_setting_check' => TRUE); foreach ($crayon_posts as $postID) { $post = get_post($postID); $post_content = $post->post_content; $post_captures = self::capture_crayons($postID, $post_content, array(), $args); if ($post_captures['has_captured'] === TRUE) { $post_obj = array(); $post_obj['ID'] = $postID; $post_obj['post_content'] = addslashes($post_captures['content']); wp_update_post($post_obj); CrayonLog::syslog("Converted Crayons in post ID {$postID} to pre tags", 'CONVERT'); } if (CrayonGlobalSettings::val(CrayonSettings::COMMENTS)) { $comments = get_comments(array('post_id' => $postID)); foreach ($comments as $comment) { $commentID = $comment->comment_ID; $comment_captures = self::capture_crayons($commentID, $comment->comment_content, array(CrayonSettings::DECODE => TRUE), $args); if ($comment_captures['has_captured'] === TRUE) { $comment_obj = array(); $comment_obj['comment_ID'] = $commentID; $comment_obj['comment_content'] = $comment_captures['content']; wp_update_comment($comment_obj); CrayonLog::syslog("Converted Crayons in post ID {$postID}, comment ID {$commentID} to pre tags", 'CONVERT'); } } } } self::refresh_posts(); }
<div id="crayon-te-content"> <?php $crayon_root_te = dirname(dirname(dirname(__FILE__))); require_once $crayon_root_te . '/crayon_wp.class.php'; require_once CrayonWP::wp_load_path(); require_once CRAYON_TE_PHP; require_once CRAYON_PARSER_PHP; CrayonSettingsWP::load_settings(); $langs = CrayonParser::parse_all(); $curr_lang = CrayonGlobalSettings::val(CrayonSettings::FALLBACK_LANG); $themes = CrayonResources::themes()->get(); $curr_theme = CrayonGlobalSettings::val(CrayonSettings::THEME); $fonts = CrayonResources::fonts()->get(); $curr_font = CrayonGlobalSettings::val(CrayonSettings::FONT); CrayonTagEditorWP::init_settings(); class CrayonTEContent { public static function select_resource($id, $resources, $current, $set_class = TRUE) { $id = CrayonSettings::PREFIX . $id; if (count($resources) > 0) { $class = $set_class ? 'class="' . CrayonSettings::SETTING . ' ' . CrayonSettings::SETTING_SPECIAL . '"' : ''; echo '<select id="' . $id . '" name="' . $id . '" ' . $class . ' ' . CrayonSettings::SETTING_ORIG_VALUE . '="' . $current . '">'; foreach ($resources as $resource) { $asterisk = $current == $resource->id() ? ' *' : ''; echo '<option value="' . $resource->id() . '" ' . selected($current, $resource->id()) . ' >' . $resource->name() . $asterisk . '</option>'; } echo '</select>'; } else { // None found, default to text box
public static function show_posts() { CrayonSettingsWP::load_settings(); $postIDs = self::load_posts(); $legacy_posts = self::load_legacy_posts(); // Avoids O(n^2) by using a hash map, tradeoff in using strval $legacy_map = array(); foreach ($legacy_posts as $legacyID) { $legacy_map[strval($legacyID)] = TRUE; } echo '<table class="crayon-table" cellspacing="0" cellpadding="0"><tr class="crayon-table-header">', '<td>', crayon__('ID'), '</td><td>', crayon__('Title'), '</td><td>', crayon__('Posted'), '</td><td>', crayon__('Modifed'), '</td><td>', crayon__('Contains Legacy Tags?'), '</td></tr>'; $posts = array(); for ($i = 0; $i < count($postIDs); $i++) { $posts[$i] = get_post($postIDs[$i]); } usort($posts, 'CrayonSettingsWP::post_cmp'); for ($i = 0; $i < count($posts); $i++) { $post = $posts[$i]; $postID = $post->ID; $title = $post->post_title; $title = !empty($title) ? $title : 'N/A'; $tr = $i == count($posts) - 1 ? 'crayon-table-last' : ''; echo '<tr class="', $tr, '">', '<td>', $postID, '</td>', '<td><a href="', $post->guid, '" target="_blank">', $title, '</a></td>', '<td>', $post->post_date, '</td>', '<td>', $post->post_modified, '</td>', '<td>', isset($legacy_map[strval($postID)]) ? '<span style="color: red;">' . crayon__('Yes') . '</a>' : crayon__('No'), '</td>', '</tr>'; } echo '</table>'; exit; }
private function load() { if (empty($this->url)) { $this->error('The specified URL is empty, please provide a valid URL.'); return; } // Try to replace the URL with an absolute path if it is local, used to prevent scripts // from executing when they are loaded. $url = $this->url; if ($this->setting_val(CrayonSettings::DECODE_ATTRIBUTES)) { $url = CrayonUtil::html_entity_decode($url); } $url = CrayonUtil::pathf($url); $site_http = CrayonGlobalSettings::site_url(); $scheme = parse_url($url, PHP_URL_SCHEME); // Try to replace the site URL with a path to force local loading if (empty($scheme)) { // No url scheme is given - path may be given as relative $url = CrayonUtil::path_slash($site_http) . CrayonUtil::path_slash($this->setting_val(CrayonSettings::LOCAL_PATH)) . $url; } $http_code = 0; // If available, use the built in wp remote http get function. if (function_exists('wp_remote_get')) { $url_uid = 'crayon_' . CrayonUtil::str_uid($url); $cached = get_transient($url_uid, 'crayon-syntax'); CrayonSettingsWP::load_cache(); if ($cached !== FALSE) { $content = $cached; $http_code = 200; } else { $response = @wp_remote_get($url, array('sslverify' => false, 'timeout' => 20)); $content = wp_remote_retrieve_body($response); $http_code = wp_remote_retrieve_response_code($response); $cache = $this->setting_val(CrayonSettings::CACHE); $cache_sec = CrayonSettings::get_cache_sec($cache); if ($cache_sec > 1 && $http_code >= 200 && $http_code < 400) { set_transient($url_uid, $content, $cache_sec); CrayonSettingsWP::add_cache($url_uid); } } } else { if (in_array(parse_url($url, PHP_URL_SCHEME), array('ssl', 'http', 'https'))) { // Fallback to cURL. At this point, the URL scheme must be valid. $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // For https connections, we do not require SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_FRESH_CONNECT, FALSE); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); if (isset($_SERVER['HTTP_USER_AGENT'])) { curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); } $content = curl_exec($ch); $error = curl_error($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } } if ($http_code >= 200 && $http_code < 400) { $this->code($content); } else { if (empty($this->code)) { // If code is also given, just use that $this->error("The provided URL ('{$this->url}'), parsed remotely as ('{$url}'), could not be accessed."); } } $this->needs_load = FALSE; }
public static function content() { CrayonSettingsWP::load_settings(); $langs = CrayonLangs::sort_by_name(CrayonParser::parse_all()); $curr_lang = CrayonGlobalSettings::val(CrayonSettings::FALLBACK_LANG); $themes = CrayonResources::themes()->get(); $curr_theme = CrayonGlobalSettings::val(CrayonSettings::THEME); $fonts = CrayonResources::fonts()->get(); $curr_font = CrayonGlobalSettings::val(CrayonSettings::FONT); CrayonTagEditorWP::init_settings(); ?> <div id="crayon-te-content" class="crayon-te"> <div id="crayon-te-bar"> <div id="crayon-te-bar-content"> <div id="crayon-te-title">Title</div> <div id="crayon-te-controls"> <a id="crayon-te-ok" href="#"><?php crayon_e('OK'); ?> </a> <span class="crayon-te-seperator">|</span> <a id="crayon-te-cancel" href="#"><?php crayon_e('Cancel'); ?> </a> </div> </div> </div> <table id="crayon-te-table" class="describe"> <tr class="crayon-tr-center"> <th><?php crayon_e('Title'); ?> </th> <td class="crayon-nowrap"><?php self::textbox('title', array('placeholder' => crayon__('A short description'))); ?> <span id="crayon-te-sub-section"> <?php self::checkbox('inline'); ?> <span class="crayon-te-section"><?php crayon_e('Inline'); ?> </span> </span> <span id="crayon-te-sub-section"> <?php self::checkbox('highlight'); ?> <span class="crayon-te-section"><?php crayon_e("Don't Highlight"); ?> </span> </span></td> </tr> <tr class="crayon-tr-center"> <th><?php crayon_e('Language'); ?> </th> <td class="crayon-nowrap"><?php self::select_resource('lang', $langs, $curr_lang); ?> <span class="crayon-te-section"><?php crayon_e('Line Range'); ?> </span> <?php self::textbox('range', array('placeholder' => crayon__('(e.g. 3-5 or 3)'))); ?> <span class="crayon-te-section"><?php crayon_e('Marked Lines'); ?> </span> <?php self::textbox('mark', array('placeholder' => crayon__('(e.g. 1,2,3-5)'))); ?> </td> </tr> <tr class="crayon-tr-center" style="text-align: center;"> <th> <div> <?php crayon_e('Code'); ?> </div> <input type="button" id="crayon-te-clear" class="secondary-primary" value="<?php crayon_e('Clear'); ?> " name="clear"/> </th> <td><textarea id="crayon-code" name="code" placeholder="<?php crayon_e('Paste your code here, or type it in manually.'); ?> "></textarea> </td> </tr> <tr class="crayon-tr-center"> <th id="crayon-url-th"><?php crayon_e('URL'); ?> </th> <td><?php self::textbox('url', array('placeholder' => crayon__('Relative local path or absolute URL'))); ?> <div id="crayon-te-url-info" class="crayon-te-info"> <?php crayon_e("If the URL fails to load, the code above will be shown instead. If no code exists, an error is shown."); echo ' '; printf(crayon__('If a relative local path is given it will be appended to %s - which is defined in %sCrayon > Settings > Files%s.'), '<span class="crayon-te-quote">' . get_home_url() . '/' . CrayonGlobalSettings::val(CrayonSettings::LOCAL_PATH) . '</span>', '<a href="options-general.php?page=crayon_settings" target="_blank">', '</a>'); ?> </div> </td> </tr> <tr> <td id="crayon-te-submit-wrapper" colspan="2" style="text-align: center;"><?php self::submit(); ?> </td> </tr> <!-- <tr>--> <!-- <td colspan="2"><div id="crayon-te-warning" class="updated crayon-te-info"></div></td>--> <!-- </tr>--> <tr> <td colspan="2"><?php $admin = isset($_GET['is_admin']) ? intval($_GET['is_admin']) : is_admin(); if (!$admin && !CrayonGlobalSettings::val(CrayonSettings::TAG_EDITOR_SETTINGS)) { exit; } ?> <hr/> <div> <h2 class="crayon-te-heading"> <?php crayon_e('Settings'); ?> </h2> </div> <div id="crayon-te-settings-info" class="crayon-te-info"> <?php crayon_e('Change the following settings to override their global values.'); echo ' <span class="', CrayonSettings::SETTING_CHANGED, '">'; crayon_e('Only changes (shown yellow) are applied.'); echo '</span><br/>'; echo sprintf(crayon__('Future changes to the global settings under %sCrayon > Settings%s won\'t affect overridden settings.'), '<a href="options-general.php?page=crayon_settings" target="_blank">', '</a>'); ?> </div> </td> </tr> <?php $sections = array('Theme', 'Font', 'Metrics', 'Toolbar', 'Lines', 'Code'); foreach ($sections as $section) { echo '<tr><th>', crayon__($section), '</th><td>'; call_user_func('CrayonSettingsWP::' . strtolower($section), TRUE); echo '</td></tr>'; } ?> </table> </div> <?php exit; }
private function load() { if (empty($this->url)) { $this->error('The specified URL is empty, please provide a valid URL.'); return; } /* Try to replace the URL with an absolute path if it is local, used to prevent scripts from executing when they are loaded. */ $url = $this->url; if ($this->setting_val(CrayonSettings::DECODE_ATTRIBUTES)) { $url = CrayonUtil::html_entity_decode($url); } $url = CrayonUtil::pathf($url); $local = FALSE; // Whether to read locally $site_http = CrayonGlobalSettings::site_url(); $site_path = CrayonGlobalSettings::site_path(); $scheme = parse_url($url, PHP_URL_SCHEME); // Try to replace the site URL with a path to force local loading if (strpos($url, $site_http) !== FALSE || strpos($url, $site_path) !== FALSE) { $url = str_replace($site_http, $site_path, $url); // Attempt to load locally $local = TRUE; $local_url = $url; } else { if (empty($scheme)) { // No url scheme is given - path may be given as relative $local_url = preg_replace('#^((\\/|\\\\)*)?#', $site_path . $this->setting_val(CrayonSettings::LOCAL_PATH), $url); $local = TRUE; } } // Try to find the file locally if ($local == TRUE) { if (($contents = CrayonUtil::file($local_url)) !== FALSE) { $this->code($contents); } else { $local = FALSE; CrayonLog::log("Local url ({$local_url}) could not be loaded", '', FALSE); } } // If reading the url locally produced an error or failed, attempt remote request if ($local == FALSE) { if (empty($scheme)) { $url = (CrayonUtil::isSecure() ? 'https://' : 'http://') . $url; } $http_code = 0; // If available, use the built in wp remote http get function, we don't need SSL if (function_exists('wp_remote_get')) { $url_uid = 'crayon_' . CrayonUtil::str_uid($url); $cached = get_transient($url_uid, 'crayon-syntax'); CrayonSettingsWP::load_cache(); if ($cached !== FALSE) { $content = $cached; $http_code = 200; } else { $response = @wp_remote_get($url, array('sslverify' => false, 'timeout' => 20)); $content = wp_remote_retrieve_body($response); $http_code = wp_remote_retrieve_response_code($response); $cache = $this->setting_val(CrayonSettings::CACHE); $cache_sec = CrayonSettings::get_cache_sec($cache); if ($cache_sec > 1 && $http_code >= 200 && $http_code < 400) { set_transient($url_uid, $content, $cache_sec); CrayonSettingsWP::add_cache($url_uid); } } } else { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // For https connections, we do not require SSL verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_FRESH_CONNECT, FALSE); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $content = curl_exec($ch); $error = curl_error($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } if ($http_code >= 200 && $http_code < 400) { $this->code($content); } else { if (empty($this->code)) { // If code is also given, just use that $this->error("The provided URL ('{$this->url}'), parsed remotely as ('{$url}'), could not be accessed."); } } } $this->needs_load = FALSE; }
public static function submit() { global $CRAYON_EMAIL; CrayonSettingsWP::load_settings(); $id = $_POST['id']; $message = $_POST['message']; $dir = CrayonResources::themes()->dirpath_for_id($id); $dest = $dir . 'tmp'; wp_mkdir_p($dest); if (is_dir($dir) && CrayonResources::themes()->exists($id)) { try { $zipFile = CrayonUtil::createZip($dir, $dest, TRUE); $result = CrayonUtil::emailFile(array('to' => $CRAYON_EMAIL, 'from' => get_bloginfo('admin_email'), 'subject' => 'Theme Editor Submission', 'message' => $message, 'file' => $zipFile)); CrayonUtil::deleteDir($dest); if ($result) { echo 1; } else { echo -3; } } catch (Exception $e) { CrayonLog::syslog($e->getMessage(), "THEME SUBMIT"); echo -2; } } else { echo -1; } exit; }
<?php // Used to send requests to db from jQuery require_once dirname(dirname(__FILE__)) . '/crayon_wp.class.php'; require_once CrayonSettingsWP::wp_load_path(); CrayonSettingsWP::load_settings(true); //echo json_encode(CrayonGlobalSettings::get()); $allowed = array(CrayonSettings::HIDE_HELP, CrayonSettings::TINYMCE_USED); //var_dump($_GET); foreach ($allowed as $allow) { if (array_key_exists($allow, $_GET)) { CrayonGlobalSettings::set($allow, $_GET[$allow]); CrayonSettingsWP::save_settings(); } }
public static function update() { // Upgrade database and settings global $CRAYON_VERSION; $settings = CrayonSettingsWP::get_settings(); if ($settings === NULL || !isset($settings[CrayonSettings::VERSION])) { return; } $version = $settings[CrayonSettings::VERSION]; $defaults = CrayonSettings::get_defaults_array(); $touched = FALSE; if ($version < '1.7.21') { $settings[CrayonSettings::SCROLL] = $defaults[CrayonSettings::SCROLL]; $touched = TRUE; } if ($version < '1.7.23' && $settings[CrayonSettings::FONT] == 'theme-font') { $settings[CrayonSettings::FONT] = $defaults[CrayonSettings::FONT]; $touched = TRUE; } if ($touched) { $settings[CrayonSettings::VERSION] = $CRAYON_VERSION; CrayonSettingsWP::save_settings($settings); } }