private static function button($args = array()) { extract($args); CrayonUtil::set_var($id, ''); CrayonUtil::set_var($class, ''); CrayonUtil::set_var($onclick, ''); CrayonUtil::set_var($title, ''); return '<a id="' . $id . '" class="button-primary ' . $class . '" onclick="' . $onclick . '">' . $title . '</a>'; }
public static function capture_crayons($wp_id, $wp_content, $extra_settings = array(), $args = array()) { extract($args); CrayonUtil::set_var($callback, NULL); CrayonUtil::set_var($callback_extra_args, NULL); CrayonUtil::set_var($ignore, TRUE); CrayonUtil::set_var($preserve_atts, FALSE); CrayonUtil::set_var($flags, NULL); CrayonUtil::set_var($skip_setting_check, FALSE); CrayonUtil::set_var($just_check, FALSE); // Will contain captured crayons and altered $wp_content $capture = array('capture' => array(), 'content' => $wp_content, 'has_captured' => FALSE); // Do not apply Crayon for posts older than a certain date. $disable_date = trim(CrayonGlobalSettings::val(CrayonSettings::DISABLE_DATE)); if ($disable_date && get_post_time('U', true, $wp_id) <= strtotime($disable_date)) { return $capture; } // Flags for which Crayons to convert $in_flag = self::in_flag($flags); CrayonLog::debug('capture for id ' . $wp_id . ' len ' . strlen($wp_content)); // Convert <pre> tags to crayon tags, if needed if ((CrayonGlobalSettings::val(CrayonSettings::CAPTURE_PRE) || $skip_setting_check) && $in_flag[CrayonSettings::CAPTURE_PRE]) { // XXX This will fail if <pre></pre> is used inside another <pre></pre> $wp_content = preg_replace_callback('#(?<!\\$)<\\s*pre(?=(?:([^>]*)\\bclass\\s*=\\s*(["\'])(.*?)\\2([^>]*))?)([^>]*)>(.*?)<\\s*/\\s*pre\\s*>#msi', 'CrayonWP::pre_tag', $wp_content); } // Convert mini [php][/php] tags to crayon tags, if needed if ((CrayonGlobalSettings::val(CrayonSettings::CAPTURE_MINI_TAG) || $skip_setting_check) && $in_flag[CrayonSettings::CAPTURE_MINI_TAG]) { $wp_content = preg_replace('#(?<!\\$)\\[\\s*(' . self::$alias_regex . ')\\b([^\\]]*)\\](.*?)\\[\\s*/\\s*(?:\\1)\\s*\\](?!\\$)#msi', '[crayon lang="\\1" \\2]\\3[/crayon]', $wp_content); $wp_content = preg_replace('#(?<!\\$)\\[\\s*(' . self::$alias_regex . ')\\b([^\\]]*)/\\s*\\](?!\\$)#msi', '[crayon lang="\\1" \\2 /]', $wp_content); } // Convert <code> to inline tags if (CrayonGlobalSettings::val(CrayonSettings::CODE_TAG_CAPTURE)) { $inline = CrayonGlobalSettings::val(CrayonSettings::CODE_TAG_CAPTURE_TYPE) === 0; $inline_setting = $inline ? 'inline="true"' : ''; $wp_content = preg_replace('#<(\\s*code\\b)([^>]*)>(.*?)</\\1[^>]*>#msi', '[crayon ' . $inline_setting . ' \\2]\\3[/crayon]', $wp_content); } if ((CrayonGlobalSettings::val(CrayonSettings::INLINE_TAG) || $skip_setting_check) && $in_flag[CrayonSettings::INLINE_TAG]) { if (CrayonGlobalSettings::val(CrayonSettings::INLINE_TAG_CAPTURE)) { // Convert inline {php}{/php} tags to crayon tags, if needed $wp_content = preg_replace('#(?<!\\$)\\{\\s*(' . self::$alias_regex . ')\\b([^\\}]*)\\}(.*?)\\{/(?:\\1)\\}(?!\\$)#msi', '[crayon lang="\\1" inline="true" \\2]\\3[/crayon]', $wp_content); } // Convert <span class="crayon-inline"> tags to inline crayon tags $wp_content = preg_replace_callback('#(?<!\\$)<\\s*span([^>]*)\\bclass\\s*=\\s*(["\'])(.*?)\\2([^>]*)>(.*?)<\\s*/\\s*span\\s*>#msi', 'CrayonWP::span_tag', $wp_content); } // Convert [plain] tags into <pre><code></code></pre>, if needed if ((CrayonGlobalSettings::val(CrayonSettings::PLAIN_TAG) || $skip_setting_check) && $in_flag[CrayonSettings::PLAIN_TAG]) { $wp_content = preg_replace_callback('#(?<!\\$)\\[\\s*plain\\s*\\](.*?)\\[\\s*/\\s*plain\\s*\\]#msi', 'CrayonFormatter::plain_code', $wp_content); } // Add IDs to the Crayons CrayonLog::debug('capture adding id ' . $wp_id . ' , now has len ' . strlen($wp_content)); $wp_content = preg_replace_callback(self::REGEX_ID, 'CrayonWP::add_crayon_id', $wp_content); CrayonLog::debug('capture added id ' . $wp_id . ' : ' . strlen($wp_content)); // Only include if a post exists with Crayon tag preg_match_all(self::regex(), $wp_content, $matches); $capture['has_captured'] = count($matches[0]) != 0; if ($just_check) { // Backticks are matched after other tags, so they need to be captured here. $result = self::replace_backquotes($wp_content); $wp_content = $result['content']; $capture['has_captured'] = $capture['has_captured'] || $result['changed']; $capture['content'] = $wp_content; return $capture; } CrayonLog::debug('capture ignore for id ' . $wp_id . ' : ' . strlen($capture['content']) . ' vs ' . strlen($wp_content)); if ($capture['has_captured']) { // Crayons found! Load settings first to ensure global settings loaded CrayonSettingsWP::load_settings(); CrayonLog::debug('CAPTURED FOR ID ' . $wp_id); $full_matches = $matches[0]; $closed_ids = $matches[1]; $closed_atts = $matches[2]; $open_ids = $matches[3]; $open_atts = $matches[4]; $contents = $matches[5]; // Make sure we enqueue the styles/scripts $enqueue = TRUE; for ($i = 0; $i < count($full_matches); $i++) { // Get attributes if (!empty($closed_atts[$i])) { $atts = $closed_atts[$i]; } else { if (!empty($open_atts[$i])) { $atts = $open_atts[$i]; } else { $atts = ''; } } // Capture attributes preg_match_all('#([^="\'\\s]+)[\\t ]*=[\\t ]*("|\')(.*?)\\2#', $atts, $att_matches); // Add extra attributes $atts_array = $extra_settings; if (count($att_matches[0]) != 0) { for ($j = 0; $j < count($att_matches[1]); $j++) { $atts_array[trim(strtolower($att_matches[1][$j]))] = trim($att_matches[3][$j]); } } if (isset($atts_array[CrayonSettings::IGNORE]) && $atts_array[CrayonSettings::IGNORE]) { // TODO(aramk) Revert to the original content. continue; } // Capture theme $theme_id = array_key_exists(CrayonSettings::THEME, $atts_array) ? $atts_array[CrayonSettings::THEME] : ''; $theme = CrayonResources::themes()->get($theme_id); // If theme not found, use fallbacks if (!$theme) { // Given theme is invalid, try global setting $theme_id = CrayonGlobalSettings::val(CrayonSettings::THEME); $theme = CrayonResources::themes()->get($theme_id); if (!$theme) { // Global setting is invalid, fall back to default $theme = CrayonResources::themes()->get_default(); $theme_id = CrayonThemes::DEFAULT_THEME; } } // If theme is now valid, change the array if ($theme) { if (!$preserve_atts || isset($atts_array[CrayonSettings::THEME])) { $atts_array[CrayonSettings::THEME] = $theme_id; } $theme->used(TRUE); } // Capture font $font_id = array_key_exists(CrayonSettings::FONT, $atts_array) ? $atts_array[CrayonSettings::FONT] : ''; $font = CrayonResources::fonts()->get($font_id); // If font not found, use fallbacks if (!$font) { // Given font is invalid, try global setting $font_id = CrayonGlobalSettings::val(CrayonSettings::FONT); $font = CrayonResources::fonts()->get($font_id); if (!$font) { // Global setting is invalid, fall back to default $font = CrayonResources::fonts()->get_default(); $font_id = CrayonFonts::DEFAULT_FONT; } } // If font is now valid, change the array if ($font) { if (!$preserve_atts || isset($atts_array[CrayonSettings::FONT])) { $atts_array[CrayonSettings::FONT] = $font_id; } $font->used(TRUE); } // Add array of atts and content to post queue with key as post ID // XXX If at this point no ID is added we have failed! $id = !empty($open_ids[$i]) ? $open_ids[$i] : $closed_ids[$i]; //if ($ignore) { $code = self::crayon_remove_ignore($contents[$i]); //} $c = array('post_id' => $wp_id, 'atts' => $atts_array, 'code' => $code); $capture['capture'][$id] = $c; CrayonLog::debug('capture finished for post id ' . $wp_id . ' crayon-id ' . $id . ' atts: ' . count($atts_array) . ' code: ' . strlen($code)); $is_inline = isset($atts_array['inline']) && CrayonUtil::str_to_bool($atts_array['inline'], FALSE) ? '-i' : ''; if ($callback === NULL) { $wp_content = str_replace($full_matches[$i], '[crayon-' . $id . $is_inline . '/]', $wp_content); } else { $wp_content = call_user_func($callback, $c, $full_matches[$i], $id, $is_inline, $wp_content, $callback_extra_args); } } } if ($ignore) { // We need to escape ignored Crayons, since they won't be captured // XXX Do this after replacing the Crayon with the shorter ID tag, otherwise $full_matches will be different from $wp_content $wp_content = self::crayon_remove_ignore($wp_content); } $result = self::replace_backquotes($wp_content); $wp_content = $result['content']; $capture['content'] = $wp_content; return $capture; }