Skip to content

Commit

Permalink
Merge pull request owen0o0#1 from owen0o0/master
Browse files Browse the repository at this point in the history
merge origin master
  • Loading branch information
jeeinn authored Dec 31, 2019
2 parents fbd3c6d + af63bc7 commit 28b3e1c
Show file tree
Hide file tree
Showing 18 changed files with 574 additions and 40 deletions.
65 changes: 46 additions & 19 deletions css/nav.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
<?php echo io_get_option('code_2_footer');?>
<!-- end 自定义代码 -->
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
add_action( 'wp_enqueue_scripts', 'fanly_remove_block_library_css', 100 );
function fanly_remove_block_library_css() {
wp_dequeue_style( 'wp-block-library' );
}
}
Binary file added images/add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions inc/ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

//图片上传
add_action('wp_ajax_nopriv_img_upload', 'io_img_upload');
add_action('wp_ajax_img_upload', 'io_img_upload');
function io_img_upload(){
$extArr = array("jpg", "png", "jpeg");
$file = $_FILES['files'];
if ( !empty( $file ) ) {
$wp_upload_dir = wp_upload_dir(); // 获取上传目录信息
$basename = $file['name'];
$baseext = pathinfo($basename, PATHINFO_EXTENSION);
$dataname = date("YmdHis_").substr(md5(time()), 0, 8) . '.' . $baseext;
$filename = $wp_upload_dir['path'] . '/' . $dataname;
rename( $file['tmp_name'], $filename ); // 将上传的图片文件移动到上传目录
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . $dataname, // 外部链接的 url
'post_mime_type' => $file['type'], // 文件 mime 类型
'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ), // 附件标题,采用去除扩展名之后的文件名
'post_content' => '', // 文章内容,留空
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename ); // 插入附件信息
if($attach_id != 0){
require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 确保包含此文件,因为wp_generate_attachment_metadata()依赖于此文件。
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>$basename))));
exit();
}else{
echo '{"status":4,"msg":"图片上传失败!"}';
exit();
}
}
}

//删除图片
add_action('wp_ajax_nopriv_img_remove', 'io_img_remove');
add_action('wp_ajax_img_remove', 'io_img_remove');
function io_img_remove(){
$attach_id = $_POST["id"];
if( empty($attach_id) ){
echo '{"status":3,"msg":"没有上传图像!"}';
exit;
}
if ( false === wp_delete_attachment( $attach_id ) )
echo '{"status":4,"msg":"图片 '.$attach_id.' 删除失败!"}';
else
echo '{"status":1,"msg":"删除成功!"}';
exit;
}

//提交文章
add_action('wp_ajax_nopriv_contribute_post', 'io_contribute');
add_action('wp_ajax_contribute_post', 'io_contribute');
function io_contribute(){
$delay = 40;
if( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < $delay ){
error('{"status":2,"msg":"您投稿也太勤快了吧,'. ($delay - ( time() - $_COOKIE["tougao"] )).'秒后再试!"}');
}

//表单变量初始化
$sites_link = isset( $_POST['tougao_sites_link'] ) ? trim(htmlspecialchars($_POST['tougao_sites_link'], ENT_QUOTES)) : '';
$sites_sescribe = isset( $_POST['tougao_sites_sescribe'] ) ? trim(htmlspecialchars($_POST['tougao_sites_sescribe'], ENT_QUOTES)) : '';
$title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
$category = isset( $_POST['tougao_cat'] ) ? $_POST['tougao_cat'] : '0';
$sites_ico = isset( $_POST['tougao_sites_ico'] ) ? trim(htmlspecialchars($_POST['tougao_sites_ico'], ENT_QUOTES)) : '';
$wechat_qr = isset( $_POST['tougao_wechat_qr'] ) ? trim(htmlspecialchars($_POST['tougao_wechat_qr'], ENT_QUOTES)) : '';
$content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';

// 表单项数据验证
if ( $category == "0" ){
error('{"status":4,"msg":"请选择分类。"}');
}
if ( !empty(get_term_children($category, 'favorites'))){
error('{"status":4,"msg":"不能选用父级分类目录。"}');
}
if ( empty($sites_sescribe) || mb_strlen($sites_sescribe) > 50 ) {
error('{"status":4,"msg":"网站描叙必须填写,且长度不得超过50字。"}');
}
if ( empty($sites_link) && empty($wechat_qr) ){
error('{"status":3,"msg":"网站链接和公众号二维码至少填一项。"}');
}
elseif ( !empty($sites_link) && !preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $sites_link)) {
error('{"status":4,"msg":"网站链接必须符合URL格式。"}');
}
if ( empty($title) || mb_strlen($title) > 30 ) {
error('{"status":4,"msg":"网站名称必须填写,且长度不得超过30字。"}');
}
//if ( empty($content) || mb_strlen($content) > 10000 || mb_strlen($content) < 6) {
// error('{"status":4,"msg":"内容必须填写,且长度不得超过10000字,不得少于6字。"}');
//}

$tougao = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
//'post_author' => 1,//用于投稿的用户ID
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'post_type' => 'sites',
//'tax_input' => array( 'favorites' => array($category) ) //游客不可用
);

// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0){
global $wpdb;
add_post_meta($status, '_sites_sescribe', $sites_sescribe);
add_post_meta($status, '_sites_link', $sites_link);
add_post_meta($status, '_sites_order', '0');
if( !empty($sites_ico))
add_post_meta($status, '_thumbnail', $sites_ico);
if( !empty($wechat_qr))
add_post_meta($status, '_wechat_qr', $wechat_qr);
wp_set_post_terms( $status, array($category), 'favorites'); //设置文章分类
setcookie("tougao", time(), time()+$delay+10);
error('{"status":1,"msg":"投稿成功!"}');
}else{
error('{"status":4,"msg":"投稿失败!"}');
}
}
function error($ErrMsg) {
echo $ErrMsg;
exit;
}
82 changes: 82 additions & 0 deletions inc/contribute-ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* 弃用,已经移至ajax.php
*/
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
header('Allow: POST');
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type: text/plain');
exit;
}

require dirname(__FILE__).'/../../../../wp-load.php';
nocache_headers();

if( isset($_COOKIE["tougao"]) && ( time() - $_COOKIE["tougao"] ) < 120 ){
error('{"status":2,"msg":"您投稿也太勤快了吧,先歇会儿!"}');
}

//表单变量初始化
$sites_link = isset( $_POST['tougao_sites_link'] ) ? trim(htmlspecialchars($_POST['tougao_sites_link'], ENT_QUOTES)) : '';
$sites_sescribe = isset( $_POST['tougao_sites_sescribe'] ) ? trim(htmlspecialchars($_POST['tougao_sites_sescribe'], ENT_QUOTES)) : '';
$title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : '';
$category = isset( $_POST['tougao_cat'] ) ? $_POST['tougao_cat'] : '0';
$sites_ico = isset( $_POST['tougao_sites_ico'] ) ? trim(htmlspecialchars($_POST['tougao_sites_ico'], ENT_QUOTES)) : '';
$wechat_qr = isset( $_POST['tougao_wechat_qr'] ) ? trim(htmlspecialchars($_POST['tougao_wechat_qr'], ENT_QUOTES)) : '';
$content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : '';

// 表单项数据验证
if ( $category == "0" ){
error('{"status":4,"msg":"请选择分类。"}');
}
if ( !empty(get_term_children($category, 'favorites'))){
error('{"status":4,"msg":"不能选用父级分类目录。"}');
}
if ( empty($sites_sescribe) || mb_strlen($sites_sescribe) > 50 ) {
error('{"status":4,"msg":"网站描叙必须填写,且长度不得超过50字。"}');
}
if ( empty($sites_link) && empty($wechat_qr) ){
error('{"status":3,"msg":"网站链接和公众号二维码至少填一项。"}');
}
elseif ( !empty($sites_link) && !preg_match('/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $sites_link)) {
error('{"status":4,"msg":"网站链接必须符合URL格式。"}');
}
if ( empty($title) || mb_strlen($title) > 30 ) {
error('{"status":4,"msg":"网站名称必须填写,且长度不得超过30字。"}');
}
//if ( empty($content) || mb_strlen($content) > 10000 || mb_strlen($content) < 6) {
// error('{"status":4,"msg":"内容必须填写,且长度不得超过10000字,不得少于6字。"}');
//}
$tougao = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
//'post_author' => 1,//用于投稿的用户ID
'post_title' => $title,
'post_content' => $content,
'post_status' => 'pending',
'post_type' => 'sites',
//'tax_input' => array( 'favorites' => array($category) ) //游客不可用
);

// 将文章插入数据库
$status = wp_insert_post( $tougao );
if ($status != 0){
global $wpdb;
add_post_meta($status, '_sites_sescribe', $sites_sescribe);
add_post_meta($status, '_sites_link', $sites_link);
add_post_meta($status, '_sites_order', '0');
if( !empty($sites_ico))
add_post_meta($status, '_thumbnail', $sites_ico);
if( !empty($wechat_qr))
add_post_meta($status, '_wechat_qr', $wechat_qr);
wp_set_post_terms( $status, array($category), 'favorites'); //设置文章分类
setcookie("tougao", time(), time()+30);
error('{"status":1,"msg":"投稿成功!"}');
}else{
error('{"status":4,"msg":"投稿失败!"}');
}

function error($ErrMsg) {
echo $ErrMsg;
exit;
}
Binary file removed inc/frame/._.DS_Store
Binary file not shown.
Binary file removed inc/frame/assets/._.DS_Store
Binary file not shown.
60 changes: 60 additions & 0 deletions inc/img-upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* WordPress 投稿页上传图片,支持游客上传
* 原文地址:https://www.iowen.cn/wordpress-visitors-upload-pictures
* 一为忆
* WebStack 导航主题
*
* 弃用,已经移至ajax.php
*/

if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
header('Allow: POST');
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type: text/plain');
exit;
}

require dirname(__FILE__).'/../../../../wp-load.php';
nocache_headers();

$extArr = array("jpg", "png", "jpeg");
$file = $_FILES['files'];
if ( !empty( $file ) ) {
$wp_upload_dir = wp_upload_dir(); // 获取上传目录信息
$basename = $file['name'];
$basesize = $file['size'];
$baseext = pathinfo($basename, PATHINFO_EXTENSION);
/* 使用前台 js 判断
if (!in_array($baseext, $extArr)) {
echo '{"status":3,"msg":"图片类型只能是jpeg,jpg,png!"}';
exit();
}
if ($basesize > (1000 * 1024)) {
echo '{"status":3,"msg":"图片大小不能超过1M"}';
exit();
}
*/
$dataname = date("YmdHis_").substr(md5(time()), 0, 8) . '.' . $baseext;
$filename = $wp_upload_dir['path'] . '/' . $dataname;
rename( $file['tmp_name'], $filename ); // 将上传的图片文件移动到上传目录
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . $dataname, // 外部链接的 url
'post_mime_type' => $file['type'], // 文件 mime 类型
'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ), // 附件标题,采用去除扩展名之后的文件名
'post_content' => '', // 文章内容,留空
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename ); // 插入附件信息
if($attach_id != 0){
require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 确保包含此文件,因为wp_generate_attachment_metadata()依赖于此文件。
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。
// 返回消息至前端
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>time()))));
exit();
}else{
echo '{"status":4,"msg":"图片上传失败!"}';
exit();
}
}
1 change: 1 addition & 0 deletions inc/inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_once get_theme_file_path() .'/inc/post-type.php';
require_once get_theme_file_path() .'/inc/fav-content.php';
require_once get_theme_file_path() .'/inc/meta-boxes.php';
require_once get_theme_file_path() .'/inc/ajax.php';


// 禁用版本修订
Expand Down
23 changes: 11 additions & 12 deletions inc/meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"sites_link" => array(
"name" => "_sites_link",
"std" => "",
"title" => "输入网址链接,需包含 http(s)://",
"title" => "输入网址链接,需包含 http(s)://<br><span style='font-weight: normal;color: crimson;margin-top: 10px;display: block;'>注意:“网址”和“公众号二维码”两者可同时填写,但是至少填一项。</span>",
"type"=>"text"),

"sites_sescribe" => array(
Expand Down Expand Up @@ -48,7 +48,7 @@ function new_meta_sites_boxes() {
global $post, $new_meta_sites_boxes;
//获取保存
foreach ($new_meta_sites_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'] . '', true);
$meta_box_value = get_post_meta($post->ID, $meta_box['name'] , true);
if ($meta_box_value != "")
//将默认值替换为已保存的值
$meta_box['std'] = $meta_box_value;
Expand All @@ -59,15 +59,15 @@ function new_meta_sites_boxes() {
echo '<h4>' . $meta_box['title'] . '</h4>';
break;
case 'text':
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<h4 style="margin-bottom: 0;">' . $meta_box['title'] . '</h4>';
echo '<span class="form-field"><input type="text" size="40" name="' . $meta_box['name'] . '" value="' . $meta_box['std'] . '" /></span><br />';
break;
case 'textarea':
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<h4 style="margin-bottom: 0;">' . $meta_box['title'] . '</h4>';
echo '<textarea id="seo-excerpt" cols="40" rows="2" name="' . $meta_box['name'] . '">' . $meta_box['std'] . '</textarea><br />';
break;
case 'radio':
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<h4 style="margin-bottom: 0;">' . $meta_box['title'] . '</h4>';
$counter = 1;
foreach ($meta_box['buttons'] as $radiobutton) {
$checked = "";
Expand All @@ -86,9 +86,8 @@ function new_meta_sites_boxes() {
break;
case 'upload':
$button_text = (isset($meta_box['button_text'])) ? $meta_box['button_text'] : 'Upload';
echo '<h4>' . $meta_box['title'] . '</h4>';
echo '<h4 style="margin-bottom: 0;">' . $meta_box['title'] . '</h4>';
echo '<input class="damiwp_url_input" style="width: 95%;margin-bottom: 10px;" type="text" id="'.$meta_box['name'].'_input" size="'.$meta_box['size'].'" value="'.$meta_box['std'].'" name="'.$meta_box['name'].'"/><br><a href="#" id="'.$meta_box['name'].'" class="dami_upload_button button">'.$button_text.'</a>';
//add_script_and_styles();
break;
}
}
Expand All @@ -102,18 +101,18 @@ function create_meta_sites_box() {
function save_sites_postdata($post_id) {
global $post, $new_meta_sites_boxes;
foreach ($new_meta_sites_boxes as $meta_box) {
if (!wp_verify_nonce($_POST[$meta_box['name'] . '_noncename'], plugin_basename(__FILE__))) {
if (!wp_verify_nonce((@$_POST[$meta_box['name'] . '_noncename']), plugin_basename(__FILE__))) {
return $post_id;
}
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) return $post_id;
} else {
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
$data = $_POST[$meta_box['name'] . ''];
if (get_post_meta($post_id, $meta_box['name'] . '') == "") add_post_meta($post_id, $meta_box['name'] . '', $data, true);
elseif ($data != get_post_meta($post_id, $meta_box['name'] . '', true)) update_post_meta($post_id, $meta_box['name'] . '', $data);
elseif ($data == "") delete_post_meta($post_id, $meta_box['name'] . '', get_post_meta($post_id, $meta_box['name'] . '', true));
$data = $_POST[$meta_box['name'] ];
if (get_post_meta($post_id, $meta_box['name'] ) == "") add_post_meta($post_id, $meta_box['name'] , $data, true);
elseif ($data != get_post_meta($post_id, $meta_box['name'] , true)) update_post_meta($post_id, $meta_box['name'] , $data);
elseif ($data == "") delete_post_meta($post_id, $meta_box['name'] , get_post_meta($post_id, $meta_box['name'] , true));
}
}
add_action('admin_menu', 'create_meta_sites_box');
Expand Down
2 changes: 1 addition & 1 deletion inc/post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function io_save_quick_edit_data($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// 验证权限,'sites' 为文章类型,默认为 'post' ,这里为我自定义的文章类型'sites'
if ( 'sites' == $_POST['post_type'] ) {
if ( 'sites' == @$_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
Expand Down
3 changes: 2 additions & 1 deletion inc/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function theme_load_scripts() {
wp_enqueue_script( 'comment-reply' );
}
}
wp_localize_script('app', 'Theme' , array(
wp_localize_script('appjs', 'theme' , array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'addico' => get_template_directory_uri() . '/images/add.png',
));
}
add_action('wp_enqueue_scripts', 'theme_load_scripts');
Expand Down
1 change: 1 addition & 0 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function trigger_resizable()
ev.preventDefault();
public_vars.$mainMenu.add(public_vars.$sidebarProfile).toggleClass('mobile-is-visible');
if($("#main-menu").hasClass('mobile-is-visible') === true){
public_vars.$sidebarMenu.removeClass('collapsed');
$(".sidebar-menu-inner").css("max-height",window.innerHeight);
ps_init();
}
Expand Down
Loading

0 comments on commit 28b3e1c

Please sign in to comment.