shaked

the_posts

Jan 19th, 2012
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Require more work but does the job.
  3. */
  4. class Posts {
  5.     const CATCH_POST_PRIORITY = -32767;
  6.     const TEXT_TO_CHANGE = 'my-plugin-text-needs-to-be-manipulated';
  7.     /*
  8.      * Manipulate our plugin's text\data
  9.      * @param array $posts
  10.      * @return array
  11.      */
  12.     public function manipulate($posts){
  13.         $newPosts = array();
  14.         foreach ($posts AS $post) {
  15.             if (false === strpos($post->post_content,self::TEXT_TO_CHANGE)
  16.                  || !self::IsPreviewMode($post->ID)){
  17.                 $newPosts[] = $post
  18.                 continue ;
  19.             }
  20.            
  21.             //remove new lines
  22.             //TODO: extract your data to your HTML (real plugin uses a View object and render's the data)
  23.             $html = str_replace("\n","",file_get_content("some.html"));
  24.             //assign back to post content
  25.             $post->post_content = str_replace(self::TEXT_TO_CHANGE,$html,$post->post_content);
  26.             $newPosts[] = $post;
  27.         }
  28.         return $newPosts;
  29.     }
  30.  
  31.     /**
  32.      * Check if we are in preview mode of current post id
  33.      * @param int $postID
  34.      * @return bool
  35.     */
  36.     private static function IsPreviewMode($postID){
  37.         return isset($_GET['preview']) && isset($_GET['p']) && $_GET['p'] != $postID;
  38.     }  
  39. }
  40.  
  41.  
  42. $posts = new Posts;
  43. add_action('the_posts' , array($posts, 'manipulate'), Posts::CATCH_POST_PRIORITY);
RAW Paste Data