/**
 * is_null_array
 *
 * @param array
 * @return bool
 * @version 1.0.0
 */
function is_null_array($arr = null)
{
    if (is_array($arr)) {
        foreach ($arr as $k => $v) {
            if ($v && !is_array($v)) {
                return false;
            }
            $t = is_null_array($v);
            if (!$t) {
                return false;
            }
        }
        return true;
    } else {
        return !$arr ? true : false;
    }
}
Beispiel #2
0
 public static function get_point_values()
 {
     static $cache = null;
     if ($cache !== null) {
         return $cache;
     }
     $values = explode(',', theme_custom_point::get_point_value('post-swap'));
     if (!is_null_array($values)) {
         $cache = array_map(function ($v) {
             $v = trim($v);
             if (is_numeric($v)) {
                 return $v;
             }
         }, $values);
     } else {
         $cache = self::get_point_values_default();
     }
     return $cache;
 }
Beispiel #3
0
 public static function post_save($post_id, $old_meta = [], $new_meta = [])
 {
     /** nothing to modify */
     if ($old_meta == $new_meta) {
         return;
     }
     /** if empty, delete */
     if (!$new_meta) {
         delete_post_meta($post_id, self::$post_meta_key['key']);
         return;
     }
     $delete = false;
     /** check */
     if (is_null_array($new_meta)) {
         delete_post_meta($post_id, self::$post_meta_key['key']);
         return;
     }
     /** update */
     update_post_meta($post_id, self::$post_meta_key['key'], $new_meta);
 }
 public static function get_posts($args = null)
 {
     global $post;
     $current_post = $post;
     $options = theme_options::get_options();
     $defaults = array('posts_per_page' => isset($options['related_post_num']) ? (int) $options['related_post_num'] : 6);
     $r = array_merge($defaults, $args);
     extract($r);
     /**
      * get the cache
      */
     $posts = (array) wp_cache_get($current_post->ID, 'theme_related_post');
     if (!is_null_array($posts)) {
         return $posts;
     }
     $tags = wp_get_post_tags($current_post->ID);
     $tags_len = count($tags);
     $surprise_num = $posts_per_page;
     $found_posts = 0;
     /* 存在tags */
     if ($tags_len) {
         for ($i = 0; $i < $tags_len; $i++) {
             $tags_array[] = $tags[$i]->term_id;
         }
         $query_args = array('tag__in' => $tags_array, 'post__not_in' => array($current_post->ID), 'posts_per_page' => $surprise_num);
         $query = new WP_Query($query_args);
         if ($query->have_posts()) {
             foreach ($query->posts as $post) {
                 $posts[] = $post;
             }
         }
         $found_posts = $query->found_posts;
         /* 发现到的文章数量 */
     }
     $surprise_num = $surprise_num - $found_posts;
     /* 计算剩余文章数量 */
     /* 当剩余文章大于0时候,调用分类目录中的文章来补充 */
     if ($surprise_num > 0) {
         $args = array('category__in' => array(theme_features::get_current_cat_id()), 'post__not_in' => array($current_post->ID), 'posts_per_page' => $surprise_num);
         $query = new WP_Query($args);
         if ($query->have_posts()) {
             foreach ($query->posts as $post) {
                 $posts[] = $post;
             }
         }
         $found_posts = $query->found_posts;
         /* 发现到的文章数量 */
         $surprise_num = $surprise_num - $found_posts;
         /* 计算剩余文章数量 */
     }
     $posts = array_filter($posts);
     wp_cache_set($current_post->ID, $posts, 'theme_related_post');
     //wp_reset_query();
     wp_reset_postdata();
     return $posts;
 }
Beispiel #5
0
 private static function process_post()
 {
     $output = [];
     $ctb = isset($_POST['ctb']) && is_array($_POST['ctb']) ? array_filter($_POST['ctb']) : null;
     /** check ctb object */
     if (empty($ctb)) {
         $output['status'] = 'error';
         $output['code'] = 'invaild_ctb_param';
         $output['msg'] = ___('Invaild contribution param.');
         die(theme_features::json_format($output));
     }
     $edit_post_id = isset($_POST['post-id']) && is_numeric($_POST['post-id']) ? (int) $_POST['post-id'] : 0;
     $edit_again = false;
     /**
      * check edit
      */
     if ($edit_post_id != 0) {
         /** set edit again */
         $edit_again = true;
         //self::set_once_published($edit_post_id);
         /**
          * check post exists
          */
         $old_post = theme_cache::get_post($edit_post_id);
         if (!$old_post || $old_post->post_type !== 'post' || !self::in_edit_post_status($old_post->post_status)) {
             die(theme_features::json_format(['status' => 'error', 'code' => 'post_not_exist', 'msg' => ___('Sorry, the post does not exist.')]));
         }
         /**
          * check post author is myself
          */
         if ($old_post->post_author != theme_cache::get_current_user_id()) {
             die(theme_features::json_format(['status' => 'error', 'code' => 'post_not_exist', 'msg' => ___('Sorry, you are not the post author, can not edit it.')]));
         }
         /**
          * check post edit lock status
          */
         $lock_user_id = self::wp_check_post_lock($edit_post_id);
         if ($lock_user_id) {
             die(theme_features::json_format(['status' => 'error', 'code' => 'post_not_exist', 'msg' => ___('Sorry, the post does not exist.')]));
         }
     }
     /**
      * post title
      */
     $post_title = isset($ctb['post-title']) && is_string($ctb['post-title']) ? trim($ctb['post-title']) : null;
     if (!$post_title) {
         $output['status'] = 'error';
         $output['code'] = 'invaild_post_title';
         $output['msg'] = ___('Please write the post title.');
         die(theme_features::json_format($output));
     }
     /**
      * post excerpt
      */
     $post_excerpt = isset($ctb['post-excerpt']) && is_string($ctb['post-excerpt']) ? trim($ctb['post-excerpt']) : null;
     /**
      * post content
      */
     $post_content = isset($ctb['post-content']) && is_string($ctb['post-content']) ? trim($ctb['post-content']) : null;
     if (!$post_content) {
         $output['status'] = 'error';
         $output['code'] = 'invaild_post_content';
         $output['msg'] = ___('Please write the post content.');
         die(theme_features::json_format($output));
     }
     /**
      * check thumbnail cover
      */
     $thumbnail_id = isset($ctb['thumbnail-id']) && is_numeric($ctb['thumbnail-id']) ? (int) $ctb['thumbnail-id'] : null;
     if (!$thumbnail_id) {
         $output['status'] = 'error';
         $output['code'] = 'invaild_thumbnail_id';
         $output['msg'] = ___('Please set an image as post thumbnail');
         die(theme_features::json_format($output));
     }
     /**
      * cats
      */
     if ($edit_post_id == 0) {
         /** new post */
         $cat_ids = isset($ctb['cats']) && is_array($ctb['cats']) ? $ctb['cats'] : null;
         if (is_null_array($cat_ids)) {
             $output['status'] = 'error';
             $output['code'] = 'invaild_cat_id';
             $output['msg'] = ___('Please select a category.');
             die(theme_features::json_format($output));
         }
         /** edit post */
     } else {
         /**
          * get all cats
          */
         $cat_id = isset($ctb['cat']) && is_numeric($ctb['cat']) ? (int) $ctb['cat'] : null;
         if (empty($cat_id)) {
             $output['status'] = 'error';
             $output['code'] = 'invaild_cat_id';
             $output['msg'] = ___('Please select a category.');
             die(theme_features::json_format($output));
         }
         $cat_ids = [];
         theme_features::get_all_cats_by_child($cat_id, $cat_ids);
     }
     /**
      * tags
      */
     $tags = isset($ctb['tags']) && is_array($ctb['tags']) ? array_filter($ctb['tags']) : [];
     if (!empty($tags)) {
         $tags = array_map(function ($tag) {
             if (!is_string($tag)) {
                 return null;
             }
             return $tag;
         }, $tags);
     }
     /**
      * post status
      */
     if (theme_cache::current_user_can('publish_posts')) {
         $post_status = 'publish';
     } else {
         $post_status = 'pending';
     }
     /*****************************
      * PASS ALL, WRITE TO DB
      *****************************/
     /** edit post */
     if ($edit_post_id != 0) {
         $post_status = self::get_update_post_status($old_post->post_status);
         $post_id = wp_update_post(['ID' => $edit_post_id, 'post_title' => $post_title, 'post_status' => $post_status, 'post_type' => $old_post->post_type, 'post_excerpt' => fliter_script($post_excerpt), 'post_content' => fliter_script($post_content), 'post_category' => $cat_ids, 'tags_input' => $tags], true);
         /**
          * insert post
          */
     } else {
         $post_id = wp_insert_post(['post_title' => $post_title, 'post_excerpt' => fliter_script($post_excerpt), 'post_content' => fliter_script($post_content), 'post_status' => $post_status, 'post_author' => theme_cache::get_current_user_id(), 'post_category' => $cat_ids, 'tags_input' => $tags], true);
     }
     /**
      * check error
      */
     if (is_wp_error($post_id)) {
         $output['status'] = 'error';
         $output['code'] = $post_id->get_error_code();
         $output['msg'] = $post_id->get_error_message();
         die(theme_features::json_format($output));
     }
     /** end post error */
     /** set post thumbnail */
     set_post_thumbnail($post_id, $thumbnail_id);
     /**
      * set attachment parent
      */
     $attach_ids = isset($ctb['attach-ids']) && is_array($ctb['attach-ids']) ? array_map('intval', array_filter($ctb['attach-ids'])) : null;
     if ($attach_ids) {
         /** set attachment post parent */
         foreach ($attach_ids as $attach_id) {
             $post = theme_cache::get_post($attach_id);
             if (!$post || $post->post_type !== 'attachment') {
                 continue;
             }
             wp_update_post(['ID' => $attach_id, 'post_parent' => $post_id]);
         }
     }
     /** end set post thumbnail */
     /**
      * if new post
      */
     if ($edit_post_id == 0) {
         /**
          * pending status
          */
         if ($post_status === 'pending') {
             $output['status'] = 'success';
             $output['msg'] = ___('Your post submitted successful, it will be published after approve in a while.');
             die(theme_features::json_format($output));
         } else {
             $output['status'] = 'success';
             $output['msg'] = sprintf(___('Congratulation! Your post has been published. You can %s or %s.'), '<a href="' . theme_cache::get_permalink($post_id) . '" title="' . theme_cache::get_the_title($post_id) . '">' . ___('View it now') . '</a>', '<a href="javascript:location.href=location.href;">' . ___('countinue to write a new post') . '</a>');
             /**
              * add point
              */
             if ($edit_again && class_exists('theme_custom_point')) {
                 $post_publish_point = theme_custom_point::get_point_value('post-publish');
                 $output['point'] = array('value' => $post_publish_point, 'detail' => ___('Post published'));
             }
             /** end point */
         }
         /** end post status */
     } else {
         $output['status'] = 'success';
         if ($old_post->post_status == 'publish') {
             $output['msg'] = ___('Your post has updated successful.') . ' <a href="' . theme_cache::get_permalink($post_id) . '" target="_blank">' . ___('Views it now') . '</a>';
         } else {
             $output['msg'] = ___('Your post has updated successful.');
         }
         die(theme_features::json_format($output));
     }
     /** end post edit */
     die(theme_features::json_format($output));
 }
Beispiel #6
0
 public static function process()
 {
     $output = [];
     theme_features::check_referer();
     theme_features::check_nonce();
     $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
     switch ($type) {
         /**
          * case upload
          */
         case 'add-cover':
             /** 
              * if not image
              */
             $filename = isset($_FILES['img']['name']) ? $_FILES['img']['name'] : null;
             $file_ext = $filename ? array_slice(explode('.', $filename), -1, 1)[0] : null;
             $file_ext = strtolower($file_ext);
             if (!in_array($file_ext, self::$file_exts)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_file_type';
                 $output['msg'] = ___('Invaild file type.');
                 die(theme_features::json_format($output));
             }
             /** rename file name */
             $_FILES['img']['name'] = theme_cache::get_current_user_id() . '-' . current_time('YmdHis') . '-' . rand(100, 999) . '.' . $file_ext;
             /** 
              * pass
              */
             require_once ABSPATH . 'wp-admin/includes/image.php';
             require_once ABSPATH . 'wp-admin/includes/file.php';
             require_once ABSPATH . 'wp-admin/includes/media.php';
             $attach_id = media_handle_upload('img', 0);
             if (is_wp_error($attach_id)) {
                 $output['status'] = 'error';
                 $output['code'] = $attach_id->get_error_code();
                 $output['msg'] = $attach_id->get_error_message();
                 die(theme_features::json_format($output));
             } else {
                 $output['status'] = 'success';
                 $output['thumbnail'] = ['url' => esc_url(self::wp_get_attachment_image_src($attach_id, 'thumbnail')[0])];
                 $output['attach-id'] = $attach_id;
                 $output['msg'] = ___('Upload success.');
                 die(theme_features::json_format($output));
             }
             break;
             /**
              * post
              */
         /**
          * post
          */
         case 'post':
             $clt = isset($_POST['clt']) && is_array($_POST['clt']) ? $_POST['clt'] : null;
             if (is_null_array($clt)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_ctb_param';
                 $output['msg'] = ___('Invaild collection param.');
                 die(theme_features::json_format($output));
             }
             /**
              * get posts
              */
             $posts = isset($clt['posts']) && is_array($clt['posts']) ? $clt['posts'] : null;
             if (empty($posts)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_posts';
                 $output['msg'] = ___('Sorry, posts can not be empty.');
                 die(theme_features::json_format($output));
             }
             /**
              * post title
              */
             $post_title = isset($clt['post-title']) && is_string($clt['post-title']) ? esc_html(trim($clt['post-title'])) : null;
             if (empty($post_title)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_post_title';
                 $output['msg'] = ___('Please write the post title.');
                 die(theme_features::json_format($output));
             }
             /**
              * check thumbnail cover
              */
             $thumbnail_id = isset($clt['thumbnail-id']) && is_numeric($clt['thumbnail-id']) ? (int) $clt['thumbnail-id'] : null;
             if (empty($thumbnail_id)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_thumbnail_id';
                 $output['msg'] = ___('Please set an image as post thumbnail');
                 die(theme_features::json_format($output));
             }
             /**
              * post content
              */
             $post_content = isset($clt['post-content']) && is_string($clt['post-content']) ? strip_tags(trim($clt['post-content']), '<del><a><b><strong><em><i>') : null;
             if (empty($post_content)) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_post_content';
                 $output['msg'] = ___('Please explain why you recommend this collection.');
                 die(theme_features::json_format($output));
             }
             /**
              * get posts template
              */
             $post_content = '<p>' . $post_content . '</p>' . self::get_preview($posts);
             /**
              * tags
              */
             $tags = isset($clt['tags']) && is_array($clt['tags']) ? $clt['tags'] : [];
             if (!empty($tags)) {
                 $tags = array_map(function ($tag) {
                     if (!is_string($tag)) {
                         return null;
                     }
                     return $tag;
                 }, $tags);
             }
             /**
              * post status
              */
             if (theme_cache::current_user_can('moderate_comments')) {
                 $post_status = 'publish';
             } else {
                 $post_status = 'pending';
             }
             /**
              * insert
              */
             $post_id = wp_insert_post(array('post_title' => $post_title, 'post_content' => fliter_script($post_content), 'post_status' => $post_status, 'post_author' => theme_cache::get_current_user_id(), 'post_category' => (array) self::get_options('cats'), 'tags_input' => $tags), true);
             if (is_wp_error($post_id)) {
                 $output['status'] = 'error';
                 $output['code'] = $post_id->get_error_code();
                 $output['msg'] = $post_id->get_error_message();
             } else {
                 /** set post thumbnail */
                 set_post_thumbnail($post_id, $thumbnail_id);
                 /**
                  * pending status
                  */
                 if ($post_status === 'pending') {
                     $output['status'] = 'success';
                     $output['msg'] = sprintf(___('Your collection submitted successful, it will be published after approve in a while. Thank you very much! How about %s again?'), '<a href="' . self::get_tabs('collection')['url'] . '">' . ___('write a new collection') . '</a>');
                     die(theme_features::json_format($output));
                 } else {
                     $output['status'] = 'success';
                     $output['msg'] = sprintf(___('Congratulation! Your post has been published. You can %s or %s.'), '<a href="' . theme_cache::get_permalink($post_id) . '" title="' . theme_cache::get_the_title($post_id) . '">' . ___('View it now') . '</a>', '<a href="' . self::get_tabs('collection')['url'] . '">' . ___('countinue to write a new collection') . '</a>');
                     /**
                      * add point
                      */
                     if (class_exists('theme_custom_point')) {
                         $post_publish_point = theme_custom_point::get_point_value('post-publish');
                         $output['point'] = array('value' => $post_publish_point, 'detail' => ___('Post published'));
                     }
                     die(theme_features::json_format($output));
                 }
             }
             break;
             /**
              * get post
              */
         /**
          * get post
          */
         case 'get-post':
             $post_id = isset($_REQUEST['post-id']) && is_numeric($_REQUEST['post-id']) ? $_REQUEST['post-id'] : null;
             if (!$post_id) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_post_id';
                 $output['msg'] = ___('Sorry, the post id is invaild.');
                 die(theme_features::json_format($output));
             }
             global $post;
             $post = theme_cache::get_post($post_id);
             if (!$post || $post->post_type !== 'post') {
                 $output['status'] = 'error';
                 $output['code'] = 'post_not_exist';
                 $output['msg'] = ___('Sorry, the post do not exist, please type another post ID.');
                 //echo(json_encode($output));
                 die(theme_features::json_format($output));
             }
             setup_postdata($post);
             $output = ['status' => 'success', 'msg' => ___('Finished get the post data.'), 'thumbnail' => ['url' => theme_functions::get_thumbnail_src($post_id), 'size' => [theme_functions::$thumbnail_size[1], theme_functions::$thumbnail_size[2]]], 'title' => theme_cache::get_the_title($post_id), 'excerpt' => html_minify(str_sub(strip_tags(trim($post->post_content)), 120, '...'))];
             wp_reset_postdata();
             die(theme_features::json_format($output));
     }
     die(theme_features::json_format($output));
 }
Beispiel #7
0
 private static function set_follow($user_id)
 {
     if ($user_id == theme_cache::get_current_user_id()) {
         return false;
     }
     $target_metas = get_user_meta($user_id, self::$user_meta_key['following']);
     /**
      * if already is follower, remove the follower and recount following count
      */
     $old_target_follower_count = (int) get_user_meta($user_id, self::$user_meta_key['follower_count'], true);
     $old_my_following_count = (int) get_user_meta(theme_cache::get_current_user_id(), self::$user_meta_key['following'], true);
     if (!is_null_array($target_metas) && in_array(theme_cache::get_current_user_id(), $target_metas)) {
         /** opera target user meta */
         delete_user_meta($user_id, self::$user_meta_key['follower'], theme_cache::get_current_user_id());
         update_user_meta($user_id, self::$user_meta_key['follower_count'], $old_target_follower_count - 1);
         /** opera current user meta */
         delete_user_meta(theme_cache::get_current_user_id(), self::$user_meta_key['following'], $user_id);
         update_user_meta(theme_cache::get_current_user_id(), self::$user_meta_key['following_count'], $old_my_following_count - 1);
         return array('user_follower_count' => $old_target_follower_count - 1, 'my_following_count' => $old_my_following_count - 1);
     } else {
         /** opera target user meta */
         add_user_meta($user_id, $user_meta_key['follower'], theme_cache::get_current_user_id());
         update_user_meta($user_id, self::$user_meta_key['follower_count'], $old_target_follower_count + 1);
         /** opera current user meta */
         add_user_meta(theme_cache::get_current_user_id(), $user_meta_key['following'], theme_cache::get_current_user_id());
         update_user_meta(theme_cache::get_current_user_id(), self::$user_meta_key['following_count'], $old_my_following_count + 1);
         return array('user_follower_count' => $old_target_follower_count + 1, 'my_following_count' => $old_my_following_count + 1);
     }
 }