Exemple #1
0
function wpjam_get_topic_messages()
{
    $current_user_id = get_current_user_id();
    $wpjam_topic_messages = get_transient('wpjam_topic_messages_' . $current_user_id);
    if ($wpjam_topic_messages === false) {
        $wpjam_topic_messages = wpjam_topic_remote_request('http://jam.wpweixin.com/api/get_topic_messages.json');
        if (is_wp_error($wpjam_topic_messages)) {
            $wpjam_topic_messages = array('unread_count' => 0, 'messages' => array());
        }
        set_transient('wpjam_topic_messages_' . $current_user_id, $wpjam_topic_messages, HOUR_IN_SECONDS);
    }
    return $wpjam_topic_messages;
}
function wpjam_topic_edit_page()
{
    global $current_admin_url;
    $topic_id = $id = isset($_GET['id']) ? $_GET['id'] : 0;
    $action = isset($_GET['action']) ? $_GET['action'] : 'add';
    $group_list = wpjam_topic_get_group_list();
    $group_list = array_merge(array(0 => ' '), $group_list);
    $group_value = isset($_GET['group']) ? $_GET['group'] : '';
    $form_fields = array('group' => array('title' => '分组', 'type' => 'select', 'options' => $group_list, 'value' => $group_value), 'title' => array('title' => '标题', 'type' => 'text'), 'content' => array('title' => '内容', 'type' => 'textarea', 'rows' => 10, 'description' => '不支持 HTML 标签,代码请放入[code][/code]中。<br />尽量输入相关的地址,否则无法分析和回答你的问题。'), 'images' => array('title' => '相关图片', 'type' => 'mu-img', 'description' => '如果文字无法描述你的问题,请添加截图。'));
    $nonce_action = $action == 'add' ? 'wpjam-topic-add' : 'wpjam-topic-edit-' . $topic_id;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $data = wpjam_get_form_post($form_fields, $nonce_action);
        $title = $data['title'];
        $content = $data['content'];
        $group = $data['group'];
        if ($images = $data['images']) {
            $image_urls = array();
            foreach ($images as $image_id) {
                $image = wp_get_attachment_image_src($image_id, 'full');
                $image_urls[] = $image[0];
            }
            $images = maybe_serialize($image_urls);
        }
        $response = wpjam_topic_remote_request('http://jam.wpweixin.com/api/topic.json', array('method' => 'POST', 'body' => compact('title', 'content', 'group', 'images', 'id')));
        if (is_wp_error($response)) {
            wpjam_admin_add_error($response->get_error_message(), 'error');
        } else {
            $redirect_url = admin_url('admin.php?page=wpjam-topics&views=mine') . '&action=reply&id=' . $response['id'];
            wp_redirect($redirect_url);
            exit;
        }
    }
    if ($action == 'edit' && $topic_id) {
        $wpjam_topic = wpjam_topic_remote_request('http://jam.wpweixin.com/api/get_topic.json?id=' . $topic_id);
        if (is_wp_error($wpjam_topic)) {
            wpjam_admin_add_error($wpjam_topics->get_error_message(), 'error');
        } else {
            if (wpjam_topic_get_openid() != $wpjam_topic['openid']) {
                wp_die('你没有权限修改该贴!');
            }
            if (time() - $wpjam_topic['time'] > 10 * MINUTE_IN_SECONDS) {
                wp_die('超过10分钟,不能再修改!');
            }
            foreach ($form_fields as $key => $form_field) {
                $form_fields[$key]['value'] = $wpjam_topic[$key];
            }
        }
    }
    $form_url = $action == 'add' ? $current_admin_url : $current_admin_url . '&action=edit&id=' . $topic_id;
    $action_text = $action == 'add' ? '提问' : '编辑';
    ?>
	<h1>我要提问</h1>
	<style type="text/css">
	.form-table { max-width:640px; }
	.form-table th {width:60px; }
	</style>
	<?php 
    wpjam_form($form_fields, $form_url, $nonce_action, $action_text);
}