Esempio n. 1
0
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);
}
Esempio n. 2
0
function wpjam_topic_user_page()
{
    global $current_admin_url;
    $action = isset($_GET['action']) ? $_GET['action'] : 'view';
    $wpjam_weixin_user = wpjam_topic_get_weixin_user();
    $nonce_action = 'wpjam-topic-user';
    $form_fields = array('openid' => array('title' => '微信昵称', 'type' => 'view', 'value' => $wpjam_weixin_user['nickname']), 'qq' => array('title' => 'QQ号', 'type' => 'number'), 'site' => array('title' => '个人站点', 'type' => 'mu-text'), 'donate' => array('title' => '收款图片', 'type' => 'image'), 'description' => array('title' => '个人说明', 'type' => 'textarea', 'class' => 'regular-text', 'rows' => 8));
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $data = wpjam_get_form_post($form_fields, $nonce_action);
        $data['site'] = maybe_serialize($data['site']);
        $response = wpjam_topic_remote_request('http://jam.wpweixin.com/api/user.json', array('method' => 'POST', 'body' => $data));
        if (is_wp_error($response)) {
            wpjam_admin_add_error($response->get_error_message(), 'error');
        } else {
            $wpjam_weixin_user = $response;
            $current_user_id = get_current_user_id();
            delete_transient('wpjam_weixin_user_' . $current_user_id);
        }
    }
    foreach ($form_fields as $key => $form_field) {
        if ($key == 'site') {
            $form_fields[$key]['value'] = isset($wpjam_weixin_user[$key]) ? maybe_unserialize(wp_unslash($wpjam_weixin_user[$key])) : '';
        } elseif ($key != 'openid') {
            $form_fields[$key]['value'] = isset($wpjam_weixin_user[$key]) ? $wpjam_weixin_user[$key] : '';
        }
    }
    if ($action == 'view') {
        foreach ($form_fields as $key => $form_field) {
            $form_fields[$key]['type'] = 'view';
            if ($key == 'site') {
                $form_fields[$key]['value'] = $form_fields[$key]['value'] ? implode("\n", $form_fields[$key]['value']) : '';
            } elseif ($key == 'donate') {
                $form_fields[$key]['value'] = $form_fields[$key]['value'] ? '<img src="' . $form_fields[$key]['value'] . '" width="207" />' : '';
            }
        }
        echo '<h1>个人资料 <a class="add-new-h2" href="' . $current_admin_url . '&action=edit">编辑</a></h1>';
        $submit_text = false;
    } else {
        echo '<h1>个人资料 <a class="add-new-h2" href="' . $current_admin_url . '&action=view">查看</a></h1>';
        $submit_text = '修改';
    }
    wpjam_form($form_fields, $current_admin_url, $nonce_action, $submit_text);
}
function wpjam_admin_get_errors()
{
    $removable_query_args = wpjam_get_removable_query_args();
    if ($removable_query_args = array_intersect($removable_query_args, array_keys($_GET))) {
        foreach ($removable_query_args as $key) {
            if ($key != 'message' && $key != 'settings-updated') {
                if ($_GET[$key] === 'true' || $_GET[$key] === '1') {
                    wpjam_admin_add_error('操作成功');
                } else {
                    wpjam_admin_add_error($_GET[$key], 'error');
                }
            }
        }
    }
    global $wpjam_errors;
    $msgs = '';
    foreach (array('updated', 'error') as $type) {
        $msg = '';
        if (isset($wpjam_errors[$type])) {
            foreach ($wpjam_errors[$type] as $message) {
                if (is_wp_error($message)) {
                    $message = $message->get_error_code() . '-' . $message->get_error_message();
                }
                $msg .= '<p><strong>' . $message . '</strong></p>';
            }
            $msg = '<div class="' . $type . '">' . $msg . '</div>';
        }
        $msgs .= $msg;
    }
    return $msgs;
}