public static function update()
 {
     global $CRAYON_VERSION;
     CrayonSettingsWP::load_settings(TRUE);
     $settings = CrayonSettingsWP::get_settings();
     if ($settings === NULL || !isset($settings[CrayonSettings::VERSION])) {
         return;
     }
     $version = $settings[CrayonSettings::VERSION];
     // Only upgrade if the version differs
     if ($version != $CRAYON_VERSION) {
         $defaults = CrayonSettings::get_defaults_array();
         $touched = FALSE;
         // Upgrade database and settings
         if (CrayonUtil::version_compare($version, '1.7.21') < 0) {
             $settings[CrayonSettings::SCROLL] = $defaults[CrayonSettings::SCROLL];
             $touched = TRUE;
         }
         if (CrayonUtil::version_compare($version, '1.7.23') < 0 && $settings[CrayonSettings::FONT] == 'theme-font') {
             $settings[CrayonSettings::FONT] = $defaults[CrayonSettings::FONT];
             $touched = TRUE;
         }
         if (CrayonUtil::version_compare($version, '1.14') < 0) {
             CrayonLog::syslog("Updated to v1.14: Font size enabled");
             $settings[CrayonSettings::FONT_SIZE_ENABLE] = TRUE;
         }
         if (CrayonUtil::version_compare($version, '1.17') < 0) {
             $settings[CrayonSettings::HIDE_HELP] = FALSE;
         }
         // Save new version
         $settings[CrayonSettings::VERSION] = $CRAYON_VERSION;
         CrayonSettingsWP::save_settings($settings);
         CrayonLog::syslog("Updated from {$version} to {$CRAYON_VERSION}");
         // Refresh to show new settings
         header('Location: ' . CrayonUtil::current_url());
         exit;
     }
 }
 public static function show_preview()
 {
     echo '<div id="content">';
     self::load_settings();
     // Run first to ensure global settings loaded
     $crayon = CrayonWP::instance();
     // Settings to prevent from validating
     $preview_settings = array(self::SAMPLE_CODE);
     // Load settings from GET and validate
     foreach ($_POST as $key => $value) {
         //	echo $key, ' ', $value , '<br/>';
         $value = stripslashes($value);
         if (!in_array($key, $preview_settings)) {
             $_POST[$key] = CrayonSettings::validate($key, $value);
         } else {
             $_POST[$key] = $value;
         }
     }
     $crayon->settings($_POST);
     if (!isset($crayon_preview_dont_override_get) || !$crayon_preview_dont_override_get) {
         $settings = array(CrayonSettings::TOP_SET => TRUE, CrayonSettings::TOP_MARGIN => 10, CrayonSettings::BOTTOM_SET => FALSE, CrayonSettings::BOTTOM_MARGIN => 0);
         $crayon->settings($settings);
     }
     // Print the theme CSS
     $theme_id = $crayon->setting_val(CrayonSettings::THEME);
     if ($theme_id != NULL) {
         echo CrayonResources::themes()->get_css($theme_id, date('U'));
     }
     $font_id = $crayon->setting_val(CrayonSettings::FONT);
     if ($font_id != NULL) {
         echo CrayonResources::fonts()->get_css($font_id);
     }
     // Load custom code based on language
     $lang = $crayon->setting_val(CrayonSettings::FALLBACK_LANG);
     $path = crayon_pf(CRAYON_UTIL_PATH . '/sample/' . $lang . '.txt', FALSE);
     if (isset($_POST[self::SAMPLE_CODE])) {
         $crayon->code($_POST[self::SAMPLE_CODE]);
     } else {
         if ($lang && @file_exists($path)) {
             $crayon->url($path);
         } else {
             $code = "\n// A sample class\nclass Human {\n\tprivate int age = 0;\n\tpublic void birthday() {\n\t\tage++;\n\t\tprint('Happy Birthday!');\n\t}\n}\n";
             $crayon->code($code);
         }
     }
     $crayon->title('Sample Code');
     $crayon->marked('5-7');
     $crayon->output($highlight = true, $nums = true, $print = true);
     echo '</div>';
     crayon_load_plugin_textdomain();
     exit;
 }
 public static function get_defaults($name = NULL, $objects = TRUE)
 {
     if (self::$default === NULL) {
         self::$default = new CrayonSettings();
     }
     if ($name === NULL) {
         // Get all settings
         if ($objects) {
             // Return array of objects
             return self::$default->get();
         } else {
             // Return associative array of name=>value
             $settings = self::$default->get();
             $defaults = array();
             foreach ($settings as $setting) {
                 $defaults[$setting->name()] = $setting->value();
             }
             return $defaults;
         }
     } else {
         // Return specific setting
         if ($objects) {
             return self::$default->get($name);
         } else {
             return self::$default->get($name)->value();
         }
     }
 }
 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;
 }
 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 settings_validate($inputs)
 {
     // Load current settings from db
     self::load_settings(TRUE);
     global $CRAYON_EMAIL;
     // When reset button is pressed, remove settings so default loads next time
     if (array_key_exists('reset', $inputs)) {
         self::clear_cache();
         return array();
     }
     // Clear the log if needed
     if (array_key_exists(self::LOG_CLEAR, $_POST)) {
         CrayonLog::clear();
     }
     // Send to admin
     if (array_key_exists(self::LOG_EMAIL_ADMIN, $_POST)) {
         CrayonLog::email(get_bloginfo('admin_email'));
     }
     // Send to developer
     if (array_key_exists(self::LOG_EMAIL_DEV, $_POST)) {
         CrayonLog::email($CRAYON_EMAIL, get_bloginfo('admin_email'));
     }
     // Clear the cache
     if (array_key_exists('crayon-cache-clear', $_POST)) {
         self::clear_cache();
     }
     // Validate inputs
     foreach ($inputs as $input => $value) {
         // Convert all array setting values to ints
         $inputs[$input] = CrayonSettings::validate($input, $value);
         // Clear cache when changed
         if ($input == CrayonSettings::CACHE && $value != CrayonGlobalSettings::val(CrayonSettings::CACHE)) {
             self::clear_cache();
         }
     }
     // If settings don't exist in input, set them to default
     $global_settings = CrayonSettings::get_defaults();
     $ignored = array(CrayonSettings::HIDE_HELP, CrayonSettings::TINYMCE_USED);
     foreach ($global_settings as $setting) {
         // XXX Ignore some settings
         if (in_array($setting->name(), $ignored)) {
             $inputs[$setting->name()] = CrayonGlobalSettings::val($setting->name());
             continue;
         }
         // If boolean setting is not in input, then it is set to FALSE in the form
         if (!array_key_exists($setting->name(), $inputs)) {
             // For booleans, set to FALSE (unchecked boxes are not sent as POST)
             if (is_bool($setting->def())) {
                 $inputs[$setting->name()] = FALSE;
             } else {
                 /*  For array settings, set the input as the value, which by default is the
                 			 default index */
                 if (is_array($setting->def())) {
                     $inputs[$setting->name()] = $setting->value();
                 } else {
                     $inputs[$setting->name()] = $setting->def();
                 }
             }
         }
     }
     return $inputs;
 }
require_once CRAYON_HIGHLIGHTER_PHP;
// These will depend on your framework
CrayonGlobalSettings::site_http('http://localhost/crayon/wp-content/plugins/crayon-syntax-highlighter/');
CrayonGlobalSettings::site_path(dirname(__FILE__));
CrayonGlobalSettings::plugin_path('http://localhost/crayon/wp-content/plugins/crayon-syntax-highlighter/');
// Should be in the header
crayon_resources();
$crayon = new CrayonHighlighter();
$crayon->code('some code');
$crayon->language('php');
$crayon->title('the title');
$crayon->marked('1-2');
$crayon->is_inline(FALSE);
// Settings
$settings = array(CrayonSettings::NUMS => FALSE, CrayonSettings::TOOLBAR => TRUE, CrayonSettings::ENQUEUE_THEMES => FALSE, CrayonSettings::ENQUEUE_FONTS => FALSE);
$settings = CrayonSettings::smart_settings($settings);
$crayon->settings($settings);
// Print the Crayon
$crayon_formatted = $crayon->output(TRUE, FALSE);
echo $crayon_formatted;
// Utility Functions
function crayon_print_style($id, $url, $version)
{
    echo '<link id="', $id, '" href="', $url, '?v=', $version, '" type="text/css" rel="stylesheet" />', "\n";
}
function crayon_print_script($id, $url, $version)
{
    echo '<script id="', $id, '" src="', $url, '?v=', $version, '" type="text/javascript"></script>', "\n";
}
function crayon_resources()
{
<?php

require_once dirname(dirname(__FILE__)) . '/crayon_wp.class.php';
require_once CrayonWP::wp_load_path();
echo '<link rel="stylesheet" href="', plugins_url(CRAYON_STYLE, dirname(__FILE__)), '?ver=', $CRAYON_VERSION, '" type="text/css" media="all" />';
echo '<div id="content">';
CrayonSettingsWP::load_settings();
// Run first to ensure global settings loaded
$crayon = CrayonWP::instance();
// Settings to prevent from validating
$preview_settings = array();
// Load settings from GET and validate
foreach ($_GET as $key => $value) {
    //	echo $key, ' ', $value , '<br/>';
    if (!in_array($key, $preview_settings)) {
        $_GET[$key] = CrayonSettings::validate($key, $value);
    }
}
$crayon->settings($_GET);
if (!isset($crayon_preview_dont_override_get) || !$crayon_preview_dont_override_get) {
    $settings = array(CrayonSettings::TOP_SET => TRUE, CrayonSettings::TOP_MARGIN => 10, CrayonSettings::BOTTOM_SET => FALSE, CrayonSettings::BOTTOM_MARGIN => 0);
    $crayon->settings($settings);
}
// Print the theme CSS
$theme_id = $crayon->setting_val(CrayonSettings::THEME);
if ($theme_id != NULL) {
    echo CrayonResources::themes()->get_css($theme_id);
}
$font_id = $crayon->setting_val(CrayonSettings::FONT);
if ($font_id != NULL) {
    echo CrayonResources::fonts()->get_css($font_id);
 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);
     }
 }