public function action_post_update_before($post)
 {
     $aliases = self::get_aliases();
     if (Options::get('tagrewriter__plurals') != NULL && Options::get('tagrewriter__plurals') == 1) {
         $pluralize = true;
     } else {
         $pluralize = false;
     }
     $tags = array();
     foreach ($post->tags as $tag) {
         if (isset($aliases[$tag])) {
             $tags[] = $aliases[$tag];
             continue;
         }
         if ($pluralize) {
             if (Tags::get_by_slug($tag . 's') != false) {
                 $tags[] = $tag . 's';
                 continue;
             } elseif (Tags::get_by_slug(rtrim($tag, 's')) != false) {
                 $tags[] = rtrim($tag, 's');
                 continue;
             }
         }
         $tags[] = $tag;
     }
     $post->tags = $tags;
 }
示例#2
0
文件: tags.php 项目: anupom/my-blog
 /**
  * Returns the count of times a tag is used.
  *
  * @param mixed The tag to count usage.
  * @return int The number of times a tag is used.
  **/
 public static function post_count($tag)
 {
     if (is_int($tag)) {
         $tag = Tags::get_by_id($tag);
     } else {
         if (is_string($tag)) {
             $tag = Tags::get_by_slug(Utils::slugify($tag));
         }
     }
     return DB::get_row('SELECT COUNT(tag_id) AS count FROM {tag2post} WHERE tag_id = ?', array($tag->id));
 }
示例#3
0
 /**
  * function delete
  * Deletes an existing post
  */
 public function delete()
 {
     $allow = true;
     $allow = Plugins::filter('post_delete_allow', $allow, $this);
     if (!$allow) {
         return;
     }
     // invoke plugins
     Plugins::act('post_delete_before', $this);
     // delete all the tags associated with this post
     foreach ($this->get_tags() as $tag_slug => $tag_text) {
         $tag = Tags::get_by_slug($tag_slug);
         Tag::detach_from_post($tag->id, $this->id);
     }
     // Delete all comments associated with this post
     if ($this->comments->count() > 0) {
         $this->comments->delete();
     }
     // Delete all info records associated with this post
     $this->info->delete_all();
     // Delete all post_tokens associated with this post
     $this->delete_tokens();
     $result = parent::deleteRecord(DB::table('posts'), array('slug' => $this->slug));
     EventLog::log(sprintf(_t('Post %1$s (%2$s) deleted.'), $this->id, $this->slug), 'info', 'content', 'habari');
     //scheduled post
     if ($this->status == Post::status('scheduled')) {
         Posts::update_scheduled_posts_cronjob();
     }
     // invoke plugins on the after_post_delete action
     Plugins::act('post_delete_after', $this);
     return $result;
 }
示例#4
0
 public function action_auth_ajax_wp_import_posts()
 {
     // get the values post'd in
     $inputs = $_POST->filter_keys(array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'category_import', 'import_index'));
     $inputs = $inputs->getArrayCopy();
     // make sure we have all our default values
     $inputs = array_merge($this->default_values, $inputs);
     // get the wpdb
     $wpdb = $this->wp_connect($inputs['db_host'], $inputs['db_name'], $inputs['db_user'], $inputs['db_pass']);
     // if we couldn't connect, error out
     if (!$wpdb) {
         EventLog::log(_t('Failed to import from "%s"', array($inputs['db_name'])));
         Session::error(_t('Failed to import from "%s"', array($inputs['db_name'])));
         echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>';
     }
     // we connected just fine, let's get moving!
     // begin a transaction. if we error out at any point, we want to roll back to before import began
     DB::begin_transaction();
     // fetch the number of posts from the wordpress database so we can batch things up
     $num_posts = $wpdb->get_value('select count(id) from ' . $inputs['db_prefix'] . 'posts');
     // figure out the LIMIT we're at
     $min = $inputs['import_index'] * IMPORT_BATCH;
     $max = min($min + IMPORT_BATCH, $num_posts);
     // for display only
     echo '<p>' . _t('Importing posts %1$d - %2$d of %3$d.', array($min, $max, $num_posts)) . '</p>';
     // get all the imported users so we can link old post authors to new post authors
     $users = DB::get_results('select user_id, value from {userinfo} where name = :name', array(':name' => 'wp_id'));
     // create an easy user map of old ID -> new ID
     $user_map = array();
     foreach ($users as $info) {
         $user_map[$info->value] = $info->user_id;
     }
     // get all the post IDs we've imported so far to make sure we don't duplicate any
     $post_map = DB::get_column('select value from {postinfo} where name = :name', array(':name' => 'wp_id'));
     // now we're ready to start importing posts
     $posts = $wpdb->get_results('select id, post_author, post_date, post_content, post_title, post_status, comment_status, post_name, post_modified, guid, post_type from ' . $inputs['db_prefix'] . 'posts order by id asc limit ' . $min . ', ' . IMPORT_BATCH);
     foreach ($posts as $post) {
         // if this post is already in the list we've imported, skip it
         if (in_array($post->id, $post_map)) {
             continue;
         }
         // set up the big taxonomy sql query
         // if this turns out to be incredibly slow we should refactor it into a big join, but they're all keys so it seems zippy enough for me
         $taxonomy_query = 'select name, slug from ' . $inputs['db_prefix'] . 'terms where term_id in ( select term_id from ' . $inputs['db_prefix'] . 'term_taxonomy where taxonomy = :taxonomy and term_taxonomy_id in ( select term_taxonomy_id from ' . $inputs['db_prefix'] . 'term_relationships where object_id = :object_id ) )';
         // get all the textual tag names for this post
         $tags = $wpdb->get_results($taxonomy_query, array(':taxonomy' => 'post_tag', ':object_id' => $post->id));
         // should we import categories as tags too?
         if ($inputs['category_import']) {
             // then do the same as above for the category taxonomy
             $categories = $wpdb->get_results($taxonomy_query, array(':taxonomy' => 'category', ':object_id' => $post->id));
         }
         // create the new post
         $p = new Post(array('title' => MultiByte::convert_encoding($post->post_title), 'content' => MultiByte::convert_encoding($post->post_content), 'user_id' => $user_map[$post->post_author], 'pubdate' => HabariDateTime::date_create($post->post_date), 'updated' => HabariDateTime::date_create($post->post_modified), 'slug' => MultiByte::convert_encoding($post->post_name)));
         // figure out the post type
         switch ($post->post_type) {
             case 'post':
                 $p->content_type = Post::type('entry');
                 break;
             case 'page':
                 $p->content_type = Post::type('page');
                 break;
             default:
                 // we're not importing other types - continue 2 to break out of the switch and the loop and continue to the next post
                 continue 2;
         }
         // figure out the post status
         switch ($post->post_status) {
             case 'publish':
                 $p->status = Post::status('published');
                 break;
             case 'future':
                 $p->status = Post::status('scheduled');
                 break;
             case 'pending':
                 // means pending-review, not pending as in scheduled
             // means pending-review, not pending as in scheduled
             case 'draft':
                 $p->status = Post::status('draft');
                 break;
             default:
                 // Post::status() returns false if it doesn't recognize the status type
                 $status = Post::status($post->post_status);
                 // store in a temp value because if you try and set ->status to an invalid value the Post class freaks
                 if ($status == false) {
                     // we're not importing statuses we don't recognize - continue 2 to break out of the switch and the loop and continue to the next post
                     continue 2;
                 } else {
                     $p->status = $status;
                 }
                 break;
         }
         // if comments are closed, disable them on the new post
         if ($post->comment_status == 'closed') {
             $p->info->comments_disabled = true;
         }
         // save the old post ID in info
         $p->info->wp_id = $post->id;
         // since we're not using it, save the old GUID too
         $p->info->wp_guid = $post->guid;
         // now that we've got all the pieces in place, save the post
         try {
             $p->insert();
             // now that the post is in the db we can add tags to it
             // first, if we want to import categories as tags, add them to the array
             if ($inputs['category_import']) {
                 $tags = array_merge($tags, $categories);
             }
             // now for the tags!
             foreach ($tags as $tag) {
                 // try to get the tag by slug, which is the key and therefore the most unique
                 $t = Tags::get_by_slug($tag->slug);
                 // if we didn't get back a tag, create a new one
                 if ($t == false) {
                     $t = Tag::create(array('term' => $tag->slug, 'term_display' => $tag->name));
                 }
                 // now that we have a tag (one way or the other), associate this post with it
                 $t->associate('post', $p->id);
             }
         } catch (Exception $e) {
             EventLog::log($e->getMessage(), 'err');
             echo '<p class="error">' . _t('There was an error importing post %s. See the EventLog for the error message.', array($post->post_title));
             echo '<p>' . _t('Rolling back changes&hellip;') . '</p>';
             // rollback all changes before we return so the import hasn't changed anything yet
             DB::rollback();
             // and return so they don't get AJAX to send them on to the next step
             return false;
         }
     }
     // if we've finished without an error, commit the import
     DB::commit();
     if ($max < $num_posts) {
         // if there are more posts to import
         // get the next ajax url
         $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_posts'));
         // bump the import index by one so we get a new batch next time
         $inputs['import_index']++;
     } else {
         // move on to importing comments
         // get the next ajax url
         $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_comments'));
         // reset the import index so we start at the first comment
         $inputs['import_index'] = 0;
     }
     // and spit out ajax to send them to the next step - posts!
     echo $this->get_ajax($ajax_url, $inputs);
 }
示例#5
0
 public function theme_mutiple_h1($theme)
 {
     $h1 = '';
     if (count($this->handler_vars) === 0) {
         $this->handler_vars = Controller::get_handler()->handler_vars;
     }
     if ($this->request->display_entries_by_date && count($this->handler_vars) > 0) {
         $date_string = '';
         $date_string .= isset($this->handler_vars['year']) ? $this->handler_vars['year'] : '';
         $date_string .= isset($this->handler_vars['month']) ? '‒' . $this->handler_vars['month'] : '';
         $date_string .= isset($this->handler_vars['day']) ? '‒' . $this->handler_vars['day'] : '';
         $h1 = '<h1>' . sprintf(_t('Posts written in %s', 'binadamu'), $date_string) . '</h1>';
     } else {
         if ($this->request->display_entries_by_tag && isset($this->handler_vars['tag'])) {
             $tag = count($this->posts) > 0 ? Tags::get_by_slug($this->handler_vars['tag'])->term_display : $this->handler_vars['tag'];
             $h1 = '<h1>' . sprintf(_t('Posts tagged with %s', 'binadamu'), htmlspecialchars($tag)) . '</h1>';
         } else {
             if ($this->request->display_search && isset($this->handler_vars['criteria'])) {
                 $h1 = '<h1>' . sprintf(_t('Search results for “%s”', 'binadamu'), htmlspecialchars($this->handler_vars['criteria'])) . '</h1>';
             }
         }
     }
     return $h1;
 }