Example #1
0
<?php

$Blogs = new PerchBlog_Blogs($API);
$Posts = new PerchBlog_Posts($API);
$message = false;
$Authors = new PerchBlog_Authors();
$Author = $Authors->find_or_create($CurrentUser);
$HTML = $API->get('HTML');
if (!$CurrentUser->has_priv('perch_blog.post.create')) {
    PerchUtil::redirect($API->app_path());
}
if (isset($_GET['id']) && $_GET['id'] != '') {
    $postID = (int) $_GET['id'];
    $Post = $Posts->find($postID, true);
    $details = $Post->to_array();
    $template = $Post->postMetaTemplate();
}
$Blog = false;
if (PERCH_RUNWAY) {
    if ($Post) {
        $Blog = $Post->get_blog();
    } else {
        if (PerchUtil::get('blog')) {
            $Blog = $Blogs->find((int) PerchUtil::get('blog'));
        }
    }
}
if (!$Blog) {
    $Blog = $Blogs->find(1);
}
$Sections = new PerchBlog_Sections();
 private function _load_author()
 {
     $Cache = PerchBlog_Cache::fetch();
     $cached_authors = $Cache->get('authors');
     if (!$cached_authors) {
         $Authors = new PerchBlog_Authors();
         $authors = $Authors->all();
         if (PerchUtil::count($authors)) {
             $cached_authors = array();
             foreach ($authors as $Author) {
                 $cached_authors[$Author->id()] = $Author;
             }
             $Cache->set('authors', $cached_authors);
         }
     }
     if ($cached_authors) {
         if (isset($cached_authors[$this->authorID()])) {
             $this->Author = $cached_authors[$this->authorID()];
             return true;
         }
     }
     return false;
 }
        $cats = $db->get_rows('SELECT * FROM ' . PERCH_DB_PREFIX . 'blog_categories');
        if (PerchUtil::count($cats)) {
            foreach ($cats as $cat) {
                $dynfields = '[]';
                if ($cat['categoryDynamicFields']) {
                    $dynfields = $cat['categoryDynamicFields'];
                }
                $NewCat = $Core_Categories->create(array('setID' => $Set->id(), 'catParentID' => 0, 'catTitle' => $cat['categoryTitle'], 'catSlug' => $cat['categorySlug'], 'catPath' => '/blog/' . $cat['categorySlug'] . '/', 'catDynamicFields' => $dynfields));
                if (is_object($NewCat)) {
                    $db->update(PERCH_DB_PREFIX . 'blog_categories', array('categoryCoreID' => $NewCat->id()), 'categoryID', $cat['categoryID']);
                }
            }
        }
    }
}
$Posts = new PerchBlog_Posts($API);
$posts = $Posts->all($Paging);
if (PerchUtil::count($posts)) {
    foreach ($posts as $Post) {
        $Post->import_legacy_categories();
        $Post->index();
    }
}
if ($Paging->is_last_page()) {
    $Sections = new PerchBlog_Sections($API);
    $Sections->update_post_counts();
    $Posts->update_category_counts();
    $Authors = new PerchBlog_Authors($API);
    $Authors->update_post_counts();
    $Settings->set('perch_blog_update', '5.0');
}
 private function _standard_where_callback($opts)
 {
     $preview_mode = self::$preview_mode;
     $db = $this->db;
     return function (PerchQuery $Query) use($opts, $preview_mode, $db) {
         // section
         if (isset($opts['section'])) {
             $Sections = new PerchBlog_Sections();
             if (is_numeric($opts['section'])) {
                 $Section = $Sections->find($opts['section']);
             } else {
                 $Section = $Sections->find_by_slug($opts['section']);
             }
             if (is_object($Section)) {
                 $Query->where[] = ' sectionID=' . $db->pdb($Section->id());
             } else {
                 $Query->where[] = ' sectionID IS NULL ';
             }
         }
         // author
         if (isset($opts['author'])) {
             $Authors = new PerchBlog_Authors();
             if (is_numeric($opts['author'])) {
                 $Author = $Authors->find($opts['author']);
             } else {
                 $Author = $Authors->find_by_slug($opts['author']);
             }
             if (is_object($Author)) {
                 $Query->where[] = ' authorID=' . $db->pdb($Author->id());
             } else {
                 $Query->where[] = ' authorID IS NULL ';
             }
         }
         // tags
         if (isset($opts['tag'])) {
             $tags = $opts['tag'];
             if (!is_array($tags)) {
                 $tags = array($tags);
             }
             $do_tag = array();
             $do_not_tag = array();
             foreach ($tags as $tag) {
                 if (substr($tag, 0, 1) == '!') {
                     $do_not_tag[] = ltrim($tag, '!');
                 } else {
                     $do_tag[] = $tag;
                 }
             }
             if (is_array($tags)) {
                 $Query->select .= ' LEFT JOIN ' . PERCH_DB_PREFIX . 'blog_posts_to_tags p2t ON tbl.postID=p2t.postID LEFT JOIN ' . PERCH_DB_PREFIX . 'blog_tags t ON p2t.tagID=t.tagID ';
                 if (PerchUtil::count($do_tag)) {
                     $Query->where[] = ' tagSlug IN (' . $db->implode_for_sql_in($do_tag) . ') ';
                 }
                 if (PerchUtil::count($do_not_tag)) {
                     $Query->where[] = ' tbl.postID NOT IN (
                             SELECT p2.postID FROM ' . $this->table . ' p2, ' . PERCH_DB_PREFIX . 'blog_posts_to_tags p2t2, ' . PERCH_DB_PREFIX . 'blog_tags t2 
                                 WHERE p2.postID=p2t2.postID  AND p2t2.tagID=t2.tagID AND t2.tagSlug IN (' . $db->implode_for_sql_in($do_not_tag) . ') 
                             )';
                 }
             }
         }
         if ($preview_mode || isset($opts['admin']) && $opts['admin']) {
             // nothing
         } else {
             $Query->where[] = 'postStatus=\'Published\' AND postDateTime<=' . $db->pdb(date('Y-m-d H:i:00')) . ' ';
         }
         return $Query;
     };
 }
<?php

$Authors = new PerchBlog_Authors($API);
$HTML = $API->get('HTML');
if (!$CurrentUser->has_priv('perch_blog.authors.manage')) {
    PerchUtil::redirect($API->app_path());
}
$authors = $Authors->all();
<?php

$Authors = new PerchBlog_Authors($API);
$HTML = $API->get('HTML');
$Form = $API->get('Form');
$message = false;
if (!$CurrentUser->has_priv('perch_blog.authors.manage')) {
    PerchUtil::redirect($API->app_path());
}
if (isset($_GET['id']) && $_GET['id'] != '') {
    $authorID = (int) $_GET['id'];
    $Author = $Authors->find($authorID);
    $details = $Author->to_array();
} else {
    $message = $HTML->failure_message('Sorry, that author could not be updated.');
    $details = false;
}
$Template = $API->get('Template');
$Template->set('blog/author.html', 'blog');
$Form->handle_empty_block_generation($Template);
$tags = $Template->find_all_tags_and_repeaters();
$Form->require_field('authorGivenName', 'Required');
$Form->require_field('authorEmail', 'Required');
$Form->set_required_fields_from_template($Template, $details);
if ($Form->submitted()) {
    $postvars = array('authorGivenName', 'authorFamilyName', 'authorEmail', 'authorSlug');
    $data = $Form->receive($postvars);
    $prev = false;
    if (isset($details['authorDynamicFields'])) {
        $prev = PerchUtil::json_safe_decode($details['authorDynamicFields'], true);
    }
Example #7
0
function perch_blog_author_for_post($id_or_slug, $opts = array(), $return = false)
{
    $id_or_slug = rtrim($id_or_slug, '/');
    $default_opts = array('template' => 'author.html', 'skip-template' => false, 'split-items' => false, 'cache' => true);
    if (is_array($opts)) {
        $opts = array_merge($default_opts, $opts);
    } else {
        $opts = $default_opts;
    }
    if ($opts['skip-template'] || $opts['split-items']) {
        $return = true;
    }
    $cache = false;
    if ($opts['cache']) {
        $cache_key = 'perch_blog_author_for_post' . md5($id_or_slug . serialize($opts));
        $cache = PerchBlog_Cache::get_static($cache_key, 10);
    }
    if ($cache) {
        if ($return) {
            return $cache;
        }
        echo $cache;
        return '';
    }
    $API = new PerchAPI(1.0, 'perch_blog');
    $BlogPosts = new PerchBlog_Posts($API);
    if (is_numeric($id_or_slug)) {
        $Post = $BlogPosts->find($id_or_slug);
    } else {
        $Post = $BlogPosts->find_by_slug($id_or_slug);
    }
    if (is_object($Post)) {
        $Authors = new PerchBlog_Authors();
        $Author = $Authors->find($Post->authorID());
        if (is_object($Author)) {
            if ($opts['skip-template']) {
                return $Author->to_array();
            }
            $Template = $API->get('Template');
            $Template->set('blog/' . $opts['template'], 'blog');
            $r = $Template->render($Author);
            if ($r != '') {
                PerchBlog_Cache::save_static($cache_key, $r);
            }
            if ($return) {
                return $r;
            }
            echo $r;
        }
    }
    return false;
}
 public function import_from_wp($wordpress_file, $format = "textile", $callback = false, $sectionID = 1)
 {
     $out = array();
     // LOAD XML
     $xml = simplexml_load_file(PerchUtil::file_path(PERCH_PATH . '/addons/apps/' . $this->api->app_id . '/import_data/' . $wordpress_file));
     // AUTHORS
     $Authors = new PerchBlog_Authors($this->api);
     foreach ($xml->channel->children('wp', true) as $tag) {
         if ($tag->getName() == 'author') {
             $data = array();
             $data['authorEmail'] = (string) $tag->author_email;
             $data['authorSlug'] = PerchUtil::urlify((string) $tag->author_display_name);
             $data['authorGivenName'] = (string) $tag->author_first_name;
             $data['authorFamilyName'] = (string) $tag->author_last_name;
             $data['authorImportRef'] = (string) $tag->author_login;
             if ($data['authorGivenName'] == '') {
                 $data['authorGivenName'] = (string) $tag->author_login;
             }
             $Author = $Authors->find_or_create_by_email((string) $tag->author_email, $data);
             if ($Author) {
                 $out[] = array('type' => 'success', 'messages' => array('Author ' . (string) $tag->author_display_name, 'Successfully imported'));
             }
         }
     }
     // POSTS
     $Posts = new PerchBlog_Posts($this->api);
     $Template = $this->api->get('Template');
     $Template->set('blog/post.html', 'blog');
     foreach ($xml->channel->item as $item) {
         $post = array();
         $post['postTitle'] = (string) $item->title;
         $post['postTags'] = '';
         $post['postLegacyURL'] = parse_url((string) $item->link, PHP_URL_PATH);
         $post_type = false;
         foreach ($item->children('wp', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'post_id':
                     $post['postImportID'] = (string) $tag;
                     break;
                 case 'post_type':
                     $post_type = (string) $tag;
                     break;
                 case 'post_date_gmt':
                     $val = strtotime((string) $tag);
                     if ($val) {
                         $post['postDateTime'] = date('Y-m-d H:i:s', $val);
                     }
                     break;
                 case 'post_date':
                     $val = strtotime((string) $tag);
                     if ($val) {
                         $post['postDateTime'] = date('Y-m-d H:i:s', $val);
                     }
                     break;
                 case 'comment_status':
                     $val = (string) $tag;
                     if ($val == 'open') {
                         $post['postAllowComments'] = '1';
                     } else {
                         $post['postAllowComments'] = '0';
                     }
                     break;
                 case 'post_name':
                     $post['postSlug'] = (string) $tag;
                     break;
                 case 'status':
                     $val = (string) $tag;
                     $post['postStatus'] = 'Draft';
                     if ($val == 'publish') {
                         $post['postStatus'] = 'Published';
                     }
                     break;
             }
         }
         // if it's not of type 'post', skip.
         if ($post_type != 'post') {
             continue;
         }
         // At this point, check we don't already have the post (as we know have the postImportID to identify it)
         if (isset($post['postImportID'])) {
             $Post = $Posts->find_by_importID($post['postImportID']);
             if (is_object($Post)) {
                 $out[] = array('type' => 'success', 'messages' => array('Post ' . $Post->postTitle(), 'Already imported'));
                 continue;
             }
         }
         foreach ($item->children('dc', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'creator':
                     $val = (string) $tag;
                     $Author = $Authors->get_one_by('authorImportRef', $val);
                     if (is_object($Author)) {
                         $post['authorID'] = $Author->id();
                     }
                     break;
             }
         }
         foreach ($item->children('content', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'encoded':
                     $raw = (string) $tag;
                     if ($format == 'textile') {
                         $html = PerchUtil::text_to_html($raw);
                         $post['postDescRaw'] = $raw;
                         $post['postDescHTML'] = $html;
                     } else {
                         $post['postDescRaw'] = $raw;
                         $post['postDescHTML'] = $raw;
                     }
                     break;
             }
         }
         foreach ($item->children('excerpt', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'encoded':
                     $raw = (string) $tag;
                     $html = PerchUtil::text_to_html($raw);
                     $fields = array();
                     $fields['excerpt'] = array();
                     if ($format == 'textile') {
                         $fields['excerpt']['raw'] = $raw;
                         $fields['excerpt']['processed'] = $html;
                     } else {
                         $fields['excerpt']['raw'] = $html;
                         $fields['excerpt']['processed'] = $html;
                     }
                     $post['postDynamicFields'] = PerchUtil::json_safe_encode($fields);
                     break;
             }
         }
         // Callbacks
         if ($callback) {
             $post = call_user_func($callback, $post, $Template);
         }
         // Section
         $post['sectionID'] = $sectionID;
         // Create the post
         $Post = $Posts->create($post, $Template);
         if (is_object($Post)) {
             $out[] = array('type' => 'success', 'messages' => array('Post ' . $Post->postTitle(), 'Successfully imported'));
             // CATEGORIES AND TAGS
             $Categories = new PerchCategories_Categories();
             $Tags = new PerchBlog_Tags($this->api);
             $postTags = array();
             $cat_ids = array();
             foreach ($item->category as $category) {
                 $attributes = $category->attributes();
                 $slug = (string) $attributes['nicename'];
                 $label = (string) $category;
                 switch ((string) $attributes['domain']) {
                     case 'post_tag':
                         $Tag = $Tags->find_or_create($slug, $label);
                         if (is_object($Tag)) {
                             $postTags[] = $Tag->tagSlug();
                             $out[] = array('type' => 'success', 'messages' => array('Tag ' . $Tag->tagSlug(), 'Successfully imported'));
                         }
                         break;
                     case 'category':
                         PerchUtil::debug("Find or create {$slug}", 'notice');
                         $Category = $Categories->find_or_create('blog/' . $slug . '/', $label);
                         if (is_object($Category)) {
                             $cat_ids[] = $Category->id();
                             $out[] = array('type' => 'success', 'messages' => array('Category ' . $label, 'Successfully imported'));
                         }
                         break;
                 }
             }
             if (PerchUtil::count($postTags)) {
                 $post['postTags'] = implode(', ', $postTags);
             }
             if (PerchUtil::count($cat_ids)) {
                 $fields['categories'] = $cat_ids;
                 $post['postDynamicFields'] = PerchUtil::json_safe_encode($fields);
             }
             $Post->Template = $Template;
             $Post->update($post);
             $Post->index($Template);
             // COMMENTS
             $Comments = new PerchBlog_Comments($this->api);
             foreach ($item->children('wp', true) as $tag) {
                 $tagName = $tag->getName();
                 if ($tagName == 'comment') {
                     if ((string) $tag->comment_type == 'pingback') {
                         continue;
                         // this is a pingback, so skip it.
                     }
                     $html = PerchUtil::text_to_html((string) $tag->comment_content);
                     $comment = array();
                     $comment['postID'] = $Post->id();
                     $comment['commentName'] = (string) $tag->comment_author;
                     $comment['commentEmail'] = (string) $tag->comment_author_email;
                     $comment['commentURL'] = (string) $tag->comment_author_url;
                     $comment['commentIP'] = ip2long((string) $tag->comment_author_IP);
                     $comment['commentDateTime'] = date('Y-m-d H:i:s', strtotime((string) $tag->comment_date_gmt));
                     $comment['commentHTML'] = $html;
                     $comment['commentSpamData'] = '';
                     $comment['commentDynamicFields'] = '';
                     if ((string) $tag->comment_approved == '1') {
                         $comment['commentStatus'] = 'LIVE';
                         $Comment = $Comments->create($comment);
                         $out[] = array('type' => 'success', 'messages' => array('Comment from ' . $comment['commentName'], 'Successfully imported'));
                     }
                 }
             }
             $Post->update_comment_count();
         }
     }
     return $out;
 }
<?php

$Authors = new PerchBlog_Authors($API);
$HTML = $API->get('HTML');
$Form = $API->get('Form');
$Form->set_name('delete');
if (!$CurrentUser->has_priv('perch_blog.authors.manage')) {
    PerchUtil::redirect($API->app_path());
}
$message = false;
if (isset($_GET['id']) && $_GET['id'] != '') {
    $Author = $Authors->find($_GET['id']);
} else {
    PerchUtil::redirect($API->app_path());
}
if ($Form->submitted()) {
    if (is_object($Author)) {
        $Author->delete();
        // clear the caches
        PerchBlog_Cache::expire_all();
        if ($Form->submitted_via_ajax) {
            echo $API->app_path() . '/authors/';
            exit;
        } else {
            PerchUtil::redirect($API->app_path() . '/authors/');
        }
    } else {
        $message = $HTML->failure_message('Sorry, that author could not be deleted.');
    }
}
$details = $Author->to_array();