Permalink
Browse files

Added a button in the settings page to display the list of posts with…

… Crayon tags

Fixed a bug causing posts with Crayon tags only in the comments (not the post content) to be ignored.
  • Loading branch information...
1 parent 63ddb9a commit bae6dbeb59a61795c99d047259ca5e563ab17698 @aramk committed Sep 26, 2012
Showing with 188 additions and 34 deletions.
  1. +31 −3 crayon_settings_wp.class.php
  2. +107 −27 crayon_wp.class.php
  3. +1 −0 global.php
  4. +7 −0 js/crayon_admin.js
  5. +10 −2 readme.txt
  6. +1 −1 util/crayon_log.class.php
  7. +1 −1 util/crayon_util.class.php
  8. +3 −0 util/list_langs.php
  9. +27 −0 util/list_posts.php
@@ -98,10 +98,15 @@ public static function init_js_settings() {
'special' => CrayonSettings::SETTING_SPECIAL,
'orig_value' => CrayonSettings::SETTING_ORIG_VALUE
);
+
+ if (is_admin()) {
+ self::$js_settings['plugins_url'] = plugins_url();
+ self::$js_settings['crayon_dir'] = CRAYON_DIR;
+ self::$js_settings['list_langs'] = CRAYON_LIST_LANGS_PHP;
+ self::$js_settings['list_posts'] = CRAYON_LIST_POSTS_PHP;
+ }
}
- //wp_localize_script('crayon_admin_js', 'CrayonSyntaxSettings', self::$js_settings);
wp_localize_script('crayon_util_js', 'CrayonSyntaxSettings', self::$js_settings);
- //CrayonThemeEditorWP::admin_scripts();
}
public static function settings() {
@@ -208,6 +213,10 @@ public static function save_settings($settings = NULL) {
}
// Crayons posts
+
+ /**
+ * This loads the posts marked as containing Crayons
+ */
public static function load_posts() {
if (self::$crayon_posts === NULL) {
// Load from db
@@ -220,10 +229,16 @@ public static function load_posts() {
return self::$crayon_posts;
}
+ /**
+ * This looks through all posts and marks those which contain Crayons
+ */
public static function scan_and_save_posts() {
- self::save_posts(CrayonWP::scan_posts());
+ self::save_posts(CrayonWP::scan_posts(TRUE, TRUE));
}
+ /**
+ * Saves the marked posts to the db
+ */
public static function save_posts($posts = NULL) {
if ($posts === NULL) {
$posts = self::$crayon_posts;
@@ -232,6 +247,9 @@ public static function save_posts($posts = NULL) {
self::load_posts();
}
+ /**
+ * Adds a post as containing a Crayon
+ */
public static function add_post($id) {
self::load_posts();
if (!in_array($id, self::$crayon_posts)) {
@@ -240,6 +258,9 @@ public static function add_post($id) {
self::save_posts();
}
+ /**
+ * Removes a post as not containing a Crayon
+ */
public static function remove_post($id) {
self::load_posts();
$key = array_search($id, self::$crayon_posts);
@@ -251,6 +272,7 @@ public static function remove_post($id) {
}
// Cache
+
public static function add_cache($name) {
self::load_cache();
if (!in_array($name, self::$cache)) {
@@ -312,6 +334,7 @@ public static function admin_init() {
self::add_field(self::GENERAL, crayon__('Tags'), 'tags');
self::add_field(self::GENERAL, crayon__('Languages'), 'langs');
self::add_field(self::GENERAL, crayon__('Files'), 'files');
+ self::add_field(self::GENERAL, crayon__('Posts'), 'posts');
self::add_field(self::GENERAL, crayon__('Tag Editor'), 'tag_editor');
self::add_field(self::GENERAL, crayon__('Misc'), 'misc');
@@ -622,6 +645,11 @@ public static function langs() {
}
}
}
+
+ public static function posts() {
+ echo '<a name="posts"></a>';
+ echo '<div id="crayon-subsection-posts-info">'.self::button(array('id'=>'show-posts', 'title'=>crayon__('Show Crayon Posts'))).'<span class="crayon-span-10"></span><a href="http://bit.ly/NQfZN5" target="_blank" class="crayon-question">' . crayon__('?') . '</a></div>' ;
+ }
public static function theme($editor = FALSE) {
$db_theme = self::$options[CrayonSettings::THEME]; // Theme name from db
View
@@ -216,13 +216,18 @@ public static function wp() {
}
// TODO put args into an array
- public static function capture_crayons($wp_id, $wp_content, $extra_settings = array(), $callback = NULL, $ignore = TRUE, $preserve_atts = FALSE, $flags = NULL) {
+ public static function capture_crayons($wp_id, $wp_content, $extra_settings = array(), $args = array()) {
+ extract($args);
+ CrayonUtil::set_var($callback, NULL);
+ CrayonUtil::set_var($ignore, TRUE);
+ CrayonUtil::set_var($preserve_atts, FALSE);
+ CrayonUtil::set_var($flags, NULL);
+
// Will contain captured crayons and altered $wp_content
$capture = array('capture' => array(), 'content' => $wp_content, 'has_captured' => FALSE);
// Flags for which Crayons to convert
$in_flag = self::in_flag($flags);
- //var_dump($in_flag);
CrayonLog::debug('capture for id ' . $wp_id . ' len ' . strlen($wp_content));
@@ -381,6 +386,8 @@ public static function capture_crayons($wp_id, $wp_content, $extra_settings = ar
/* Search for Crayons in posts and queue them for creation */
public static function the_posts($posts) {
+ var_dump($posts); exit;
+
CrayonLog::debug('the_posts');
// Whether to enqueue syles/scripts
@@ -389,7 +396,7 @@ public static function the_posts($posts) {
self::init_tags_regex();
$crayon_posts = CrayonSettingsWP::load_posts(); // Loads posts containing crayons
-
+
// Search for shortcode in posts
foreach ($posts as $post) {
$wp_id = $post->ID;
@@ -399,10 +406,9 @@ public static function the_posts($posts) {
if (CrayonGlobalSettings::val(CrayonSettings::SAFE_ENQUEUE) && is_page($wp_id)) {
CrayonGlobalSettings::set(CrayonSettings::ENQUEUE_THEMES, false);
CrayonGlobalSettings::set(CrayonSettings::ENQUEUE_FONTS, false);
- } else {
- // Only include crayon posts
- continue;
}
+ // Only include crayon posts
+ continue;
}
$id_str = strval($wp_id);
@@ -801,18 +807,52 @@ public static function wp_head() {
}
}
- public static function save_post($id, $post) {
- CrayonSettingsWP::load_settings(TRUE);
- if (wp_is_post_revision($id)) {
- // Ignore revisions
- return;
- }
+ public static function save_post($update_id, $post) {
+// if (wp_is_post_revision($id)) {
+// // Ignore revisions
+// return;
+// }
+
+ $id = $post->ID;
+
self::init_tags_regex();
+
if (preg_match(self::$tags_regex, $post->post_content)) {
CrayonSettingsWP::add_post($id);
} else {
- CrayonSettingsWP::remove_post($id);
+ $found = FALSE;
+ CrayonSettingsWP::load_settings(TRUE);
+ if (CrayonGlobalSettings::val(CrayonSettings::COMMENTS)) {
+ $comments = get_comments(array('post_id' => $id));
+ foreach ($comments as $comment) {
+ $found = self::save_comment($comment->comment_ID, NULL, $comment);
+ if ($found) {
+ break;
+ }
+ }
+ }
+ if (!$found) {
+ CrayonSettingsWP::remove_post($id);
+ }
+ }
+ }
+
+ public static function save_comment($id, $is_spam = NULL, $comment = NULL) {
+ self::init_tags_regex();
+
+ if ($comment == NULL) {
+ $comment = get_comment($id);
}
+
+ $content = $comment->comment_content;
+ $post_id = $comment->comment_post_ID;
+
+ if (preg_match(self::$tags_regex, $content)) {
+ CrayonSettingsWP::add_post($post_id);
+ return TRUE;
+ } else {
+ return FALSE;
+ }
}
public static function crayon_theme_css() {
@@ -836,8 +876,11 @@ public static function init($request) {
crayon_load_plugin_textdomain();
}
- // Return an array of post IDs where crayons occur
- public static function scan_posts($init_regex = TRUE) {
+ /**
+ * Return an array of post IDs where crayons occur.
+ * Comments are ignored by default.
+ */
+ public static function scan_posts($init_regex = TRUE, $check_comments = FALSE) {
if ($init_regex) {
// We can skip this if needed
self::init_tags_regex();
@@ -846,8 +889,20 @@ public static function scan_posts($init_regex = TRUE) {
$crayon_posts = array();
$query = new WP_Query(array('post_type' => 'any', 'suppress_filters' => TRUE, 'posts_per_page' => '-1'));
foreach ($query->posts as $post) {
+ $post_id = $post->ID;
if (preg_match(self::$tags_regex, $post->post_content)) {
- $crayon_posts[] = $post->ID;
+ $crayon_posts[] = $post_id;
+ } else if ($check_comments) {
+ CrayonSettingsWP::load_settings(TRUE);
+ if (CrayonGlobalSettings::val(CrayonSettings::COMMENTS)) {
+ $comments = get_comments(array('post_id' => $post_id));
+ foreach ($comments as $comment) {
+ if (preg_match(self::$tags_regex, $comment->comment_content)) {
+ $crayon_posts[] = $post_id;
+ break;
+ }
+ }
+ }
}
}
return $crayon_posts;
@@ -924,27 +979,46 @@ public static function post_get_excerpt($e) {
/**
* 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, since it also updates posts and may clash.
+ * XXX: Do NOT call this while updating posts or comments, it may cause an infinite loop or fail
*/
public static function convert_tags($just_check = FALSE) {
$result = self::can_convert_tags(TRUE);
$crayon_posts = $result[0];
$flags = $result[1];
+ $args = array(
+ 'callback' => 'CrayonWP::capture_replace_pre',
+ 'ignore' => FALSE,
+ 'preserve_atts' => TRUE,
+ 'flags' => $flags
+ );
+
foreach ($crayon_posts as $postID) {
$post = get_post($postID);
$post_content = $post->post_content;
- $captures = self::capture_crayons($postID, $post_content, array(), 'CrayonWP::capture_replace_pre', FALSE, TRUE, $flags);
+ $post_captures = self::capture_crayons($postID, $post_content, array(), $args);
+
$post_obj = array();
$post_obj['ID'] = $postID;
- $post_obj['post_content'] = $captures['content'];
+ $post_obj['post_content'] = $post_captures['content'];
+ wp_update_post($post_obj);
+ CrayonLog::syslog("Converted Crayons in post ID $postID to pre tags", 'CONVERT');
- //var_dump($post_obj);
+ 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);
+
+ $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');
+ }
+ }
- //CrayonLog::log("Converted Crayons in post ID $postID to pre tags", 'CONVERT');
- wp_update_post($post_obj);
}
-// exit;
}
public static function can_convert_tags($return = FALSE) {
@@ -953,11 +1027,13 @@ public static function can_convert_tags($return = FALSE) {
$flags = self::tag_bit(CrayonSettings::CAPTURE_MINI_TAG) |
self::tag_bit(CrayonSettings::INLINE_TAG) |
self::tag_bit(CrayonSettings::PLAIN_TAG);
- self::init_tags_regex(TRUE,$flags);
-
- // These posts will contian the old tag markup
- $crayon_posts = self::scan_posts(false);
+ self::init_tags_regex(TRUE, $flags);
+ // These posts (or comments) will contain the old tag markup
+ $crayon_posts = self::scan_posts(FALSE, TRUE);
+
+ // Reset to all flags for other queries
+ self::init_tags_regex(TRUE);
if (count($crayon_posts) > 0) {
if ($return) {
return array($crayon_posts, $flags);
@@ -1019,6 +1095,10 @@ public static function capture_replace_pre($capture, $original, $id, $is_inline,
} else {
// For marking a post as containing a Crayon
add_action('save_post', 'CrayonWP::save_post', 10, 2);
+ if (CrayonGlobalSettings::val(CrayonSettings::COMMENTS)) {
+ //add_action('comment_post', 'CrayonWP::save_comment', 10, 2);
+ add_action('edit_comment', 'CrayonWP::save_comment', 10, 2);
+ }
}
}
View
@@ -105,6 +105,7 @@
define('CRAYON_TIMER_PHP', CRAYON_UTIL_DIR . 'crayon_timer.class.php');
define('CRAYON_LOG_PHP', CRAYON_UTIL_DIR . 'crayon_log.class.php');
define('CRAYON_LIST_LANGS_PHP', CRAYON_UTIL_DIR . 'list_langs.php');
+define('CRAYON_LIST_POSTS_PHP', CRAYON_UTIL_DIR . 'list_posts.php');
define('CRAYON_PREVIEW_PHP', CRAYON_UTIL_DIR . 'preview.php');
define('CRAYON_AJAX_PHP', CRAYON_UTIL_DIR . 'ajax.php');
View
@@ -89,6 +89,13 @@
preview_toggle();
});
}
+
+ $('#show-posts').click(function() {
+ var url = settings.plugins_url + '/' + settings.crayon_dir + settings.list_posts + '?wp_load=' + settings.wp_load;
+ $.get(url, function(data) {
+ $('#crayon-subsection-posts-info').html(data);
+ });
+ });
// Convert
$('#crayon-settings-form input').live('focusin focusout mouseup', function() {
View
@@ -22,7 +22,7 @@ It also supports some neat features like:
* Copy/paste code
* Open code in a new window (popup)
* bbPress 2 support
-* <a href="http://bit.ly/ReRr0i" target="_blank">Converting legacy code in blog posts to &lt;pre&gt;</a>
+* <a href="http://bit.ly/ReRr0i" target="_blank">Converting legacy code in blog posts/comments to &lt;pre&gt;</a>
* Remote request caching
* <a href="http://ak.net84.net/projects/mixed-language-highlighting-in-crayon/" target="_blank">Mixed Language Highlighting</a> in a single Crayon
* <a href="http://ak.net84.net/projects/mini-tags-in-crayon/" target="_blank">Mini Tags</a> like [php][/php]
@@ -39,6 +39,7 @@ It also supports some neat features like:
* Toggled toolbar
* Striped lines
* Line marking (for important lines)
+* <a href="http://ak.net84.net/crayon/line-ranges-in-crayon/" target="_blank">Line ranges (showing only parts of the code)</a>
* Starting line number (default is 1)
* Local directory to search for local files
* File extension detection
@@ -121,6 +122,10 @@ See the <a href="http://ak.net84.net/projects/crayon-language-file-specification
These are helpful for discovering new features.
+* <a href="http://ak.net84.net/crayon/internal-post-management-crayon/" target="_blank">Internal Post Management in Crayon</a>
+* <a href="http://ak.net84.net/crayon/converting-legacy-tags-to-pre/" target="_blank">Converting Legacy Tags to &lt;pre&gt;</a>
+* <a href="http://ak.net84.net/crayon/crayon-with-bbpress/" target="_blank">Crayon with bbPress</a>
+* <a href="http://ak.net84.net/crayon/line-ranges-in-crayon/" target="_blank">Line Ranges in Crayon</a>
* <a href="http://ak.net84.net/projects/crayon-tag-editor/" target="_blank">Crayon Tag Editor</a>
* <a href="http://ak.net84.net/projects/mixed-language-highlighting-in-crayon/" target="_blank">Mixed Language Highlighting in Crayon</a>
* <a href="http://ak.net84.net/projects/mini-tags-in-crayon/" target="_blank">Mini Tags And Plain Tags In Crayon</a>
@@ -131,6 +136,7 @@ These are helpful for discovering new features.
A handful of articles from others written about Crayon, thanks guys!
+* <a href="http://bbpress.org/forums/topic/state-of-syntax-highlighter-support-in-bbpress-2/" target="_blank">State of syntax highlighter support in bbPress 2</a>
* <a href="http://www.techbrunch.fr/informations/plugin-wordpress-afficher-code-source/" target="_blank">The ultimate plugin for displaying code in WordPress (French)</a>
* <a href="http://www.trynull.com/2012/06/15/finally-wordpress-code-syntax-highlighting-that-works/" target="_blank">Finally!�A WordPress code syntax highlighting that works</a>
* <a href="http://selfpwnt.com/crayon-syntax-highlighter-and-its-studly-author/" target="_blank">Crayon Syntax Highlighter (and its studly author)</a>
@@ -239,7 +245,9 @@ Contact me at http://twitter.com/crayonsyntax or crayon.syntax@gmail.com.
== Changelog ==
= 1.12 =
-* Added ability to convert all legacy Crayon tags in blog posts to &lt;pre&gt; tags, retaining all attributes. These are the accepted standard for the Tag Editor and they're backwards compatible (if Crayon is off or you're using another highlighter).
+* Added ability to convert all legacy Crayon tags in blog posts and comments to &lt;pre&gt; tags, retaining all attributes. These are the accepted standard for the Tag Editor and they're backwards compatible (if Crayon is off or you're using another highlighter).
+* Added a button in the settings page to display the list of posts with Crayon tags
+* Fixed a bug causing posts with Crayon tags only in the comments (not the post content) to be ignored.
* Added Go Language
* Added 4 new fonts suggested by <a href="http://andrealazzarotto.com/" target="_blank">Andrea Lazzarotto</a>
* Added Ado theme thanks to <a href="http://adostudio.it/" target="_blank">adostudio.it</a>
Oops, something went wrong.

0 comments on commit bae6dbe

Please sign in to comment.