-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
398 lines (378 loc) · 18.2 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
function custom_enqueue_styles() {
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-scripts', get_template_directory_uri() . '/js/bootstrap.min.js');
wp_enqueue_script('load-image', get_template_directory_uri() . '/js/load-image.all.min.js');
wp_enqueue_script('mobile-detect', get_template_directory_uri() . '/js/mobile-detect.min.js');
wp_enqueue_script('waitMe_js', get_template_directory_uri() . '/js/waitMe.min.js');
wp_enqueue_style('waitMe_css', get_template_directory_uri() . '/css/waitMe.min.css');
wp_enqueue_script('main', get_template_directory_uri() . '/js/main.js');
wp_localize_script('main', 'WCTPE', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('mxp-ajax-nonce'),
));
wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');
wp_enqueue_style('custom_css', get_template_directory_uri() . '/css/style.css');
}
add_action('wp_enqueue_scripts', 'custom_enqueue_styles');
//優化主題樣式相關
function optimize_theme_setup() {
//整理head資訊
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
add_filter('the_generator', '__return_false');
add_filter('show_admin_bar', '__return_false');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'feed_links_extra', 3);
//移除css, js資源載入時的版本資訊
function remove_version_query($src) {
if (strpos($src, 'ver=')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('style_loader_src', 'remove_version_query', 999);
add_filter('script_loader_src', 'remove_version_query', 999);
add_filter('widget_text', 'do_shortcode');
//加上縮圖
add_theme_support('post-thumbnails');
add_theme_support('title-tag');
add_theme_support('custom-logo');
$defaults = array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => true,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
'video' => false,
'video-active-callback' => 'is_front_page',
);
add_theme_support('custom-header', $defaults);
$defaults = array(
'default-image' => '',
'default-preset' => 'default',
'default-position-x' => 'left',
'default-position-y' => 'top',
'default-size' => 'auto',
'default-repeat' => 'repeat',
'default-attachment' => 'scroll',
'default-color' => '',
'wp-head-callback' => '_custom_background_cb',
'admin-head-callback' => '',
'admin-preview-callback' => '',
);
add_theme_support('custom-background', $defaults);
register_nav_menus(array(
'primary' => '主選單',
'secondary' => '次選單',
));
}
add_action('after_setup_theme', 'optimize_theme_setup');
function wctpe2018_form_shortcode($atts) {
extract(shortcode_atts(array(
'id' => 'wctpe2018_form',
'class' => 'wctpe2018_form',
'title' => 'Share it! / 來投稿吧!',
'title_tag' => 'h3',
), $atts));
$content = "";
$user_name = isset($_COOKIE['user_name']) ? $_COOKIE['user_name'] : "";
$user_website = isset($_COOKIE['user_website']) ? $_COOKIE['user_website'] : "";
$user_posttitle = isset($_COOKIE['user_posttitle']) ? $_COOKIE['user_posttitle'] : "";
$user_email = isset($_COOKIE['user_email']) ? $_COOKIE['user_email'] : "";
$content .= "<p class='text-center'>WordCamp Taipei 2018 帶給你什麼感覺呢?</p><p class='text-center'>想拿隱藏版 WAPUU 貼紙或是徵才或是與我們保持聯繫嗎?</p><p class='text-center'>大方的在下方表單留下你的大會活動的回憶吧!</p><p class='text-center'>What do you think about WordCamp Taipei 2018?</p><p class='text-center'>You could share your experience at #WCTPE, introduce yourself, find partners, leave hiring information, anything you could think of!</p><p class='text-center'>If you left a message on the board, you could get a hidden wapuu sticker at registration desk after 1 PM.</p>";
$content .= '<div class="' . esc_attr($class) . '" id="chun-' . esc_attr($id) . '">';
if ($title !== '') {
$content .= '<' . esc_attr($title_tag) . '>' . esc_html($title) . '</' . esc_attr($title_tag) . '>';
}
$content .= '<form method="POST" action="" id="wctpe2018_form">';
$content .= '<div class="qa-field"><span class="qa-desc">Name*</span><input type="text" id="qa-name" placeholder="Name / 稱呼" value="' . esc_attr($user_name) . '" name="mxp-name"/></div>';
$content .= '<div class="qa-field"><span class="qa-desc">Email*</span><input type="text" id="qa-email" placeholder="Email / 信箱" value="' . esc_attr($user_email) . '" name="mxp-email"/></div>';
$content .= '<div class="qa-field"><span class="qa-desc">Website</span><input type="text" id="qa-website" placeholder="Website / 網站 http(s)://..." value="' . esc_attr($user_website) . '" name="mxp-website"/></div>';
$content .= '<div class="qa-field"><span class="qa-desc">Who are you? / 你是什麼人呢?*</span><input type="text" id="qa-title" placeholder="Tell us here! / 跟我們說吧!" value="' . esc_attr($user_posttitle) . '" name="mxp-title"/></div>';
$content .= '<div class="qa-field"><span class="qa-desc">What do you want to share? / 想說什麼呢?*</span><textarea id="qa-content" placeholder="Message / 內文" value="" name="mxp-content"></textarea></div>';
$content .= '<div class="qa-field"><span class="qa-desc">Image*</span><input type="file" id="qa-image" accept="image/*"/></div>';
$content .= '<div class="qa-field"><input type="hidden" id="qa-image-proc" value="" name="mxp-image"/></div>';
$content .= '<div class="qa-field "><input type="hidden" value="' . wp_create_nonce('mxp-wctpe2018form-nonce') . '" name="mxp-postkey"/></div>';
$content .= '<div id="img-preview"></div>';
$content .= '</form>';
$content .= '<button id="submit_btn">Submit</button>';
$content .= '</div>';
return $content;
}
add_shortcode('wctpe2018_form', 'wctpe2018_form_shortcode');
function wctpe2018_display_shortcode($atts) {
extract(shortcode_atts(array(
'id' => '',
), $atts));
if ($id == "") {
return '';
}
$datetime = get_post_meta($id, 'wctp2018-post-datetime', true);
$email = get_post_meta($id, 'wctp2018-author-email', true);
$website = get_post_meta($id, 'wctpe2018-author-website', true);
$name = get_post_meta($id, 'wctp2018-author-name', true);
$title = get_post_meta($id, 'wctp2018-post-title', true);
$content = get_post_meta($id, 'wctp2018-post-content', true);
$image_full = get_post_meta($id, 'wctp2018-post-image-full', true);
$image_large = get_post_meta($id, 'wctp2018-post-image-large', true);
$show_content = '<div class="wctpe2018 posts row" id="post-' . esc_attr($id) . '"><div class="col-md-5 m_b_20">';
if ($website != '') {
if (substr($website, 0, 4) != "http") {
$website = "http://" . $website;
}
$show_content .= '<div class="post-field"><span class="post-desc">Name:</span><a href="' . esc_attr($website) . '">' . esc_html($name) . '</a></div>';
} else {
$show_content .= '<div class="post-field"><span class="post-desc">Name:</span>' . esc_html($name) . '</div>';
}
if ($email != 'nobody') {
$show_content .= '<div class="post-field"><span class="post-desc">Email:</span>' . esc_html($email) . '</div>';
}
$show_content .= '<div class="post-field"><span class="post-desc">Title:</span>' . esc_html($title) . '</div>';
$show_content .= '<div class="post-field"><span class="post-desc">Message:</span>' . $content . '</div>
<div><span id="FBShare">Share to Facebook</span><span id="TwitterShare">Share to Twitter</span></div>
</div>';
$show_content .= '<div class="col-md-7"><div class="post-field"><img src="' . esc_attr($image_large) . '"/></div></div>';
// $show_content .= '';
$show_content .= '</div>';
return $show_content;
}
add_shortcode('wctpe2018_display', 'wctpe2018_display_shortcode');
function insert_social_tags_in_head() {
global $post;
if (!is_single()) {
return;
}
$datetime = get_post_meta($post->ID, 'wctp2018-post-datetime', true);
$email = get_post_meta($post->ID, 'wctp2018-author-email', true);
$website = get_post_meta($post->ID, 'wctpe2018-author-website', true);
$name = get_post_meta($post->ID, 'wctp2018-author-name', true);
$title = get_post_meta($post->ID, 'wctp2018-post-title', true);
$content = get_post_meta($post->ID, 'wctp2018-post-content', true);
$image_full = get_post_meta($post->ID, 'wctp2018-post-image-full', true);
$image_large = get_post_meta($post->ID, 'wctp2018-post-image-large', true);
$thumbnail_src = get_template_directory_uri() . '/img/wapuu-267x300.png';
if (has_post_thumbnail($post->ID)) {
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
$thumbnail_src = $thumbnail_src[0];
}
?>
<meta property="og:title" content="<?php echo esc_attr("{$title} - {$name}"); ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="<?php echo get_permalink($post->ID); ?>" />
<meta property="og:image" content="<?php echo esc_attr($thumbnail_src); ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" />
<meta property="og:description" content="<?php echo esc_attr($content); ?>" />
<meta property="article:tag" content="WCTPE2018" />
<meta property="article:tag" content="WordCamp Taipei 2018" />
<meta property="article:publisher" content="https://www.facebook.com/WordCamp.Taipei/" />
<meta property="article:published_time" content="<?php echo date('c', $datetime); ?>" />
<meta property="article:modified_time" content="<?php echo date('c', $datetime); ?>" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@WordCampTaipei" />
<meta name="twitter:domain" content="<?php echo get_bloginfo('url'); ?>" />
<meta name="twitter:title" content="<?php echo esc_attr("{$title} - {$name}"); ?>" />
<meta name="twitter:description" content="<?php echo esc_attr($content); ?>" />
<meta name="twitter:image" content="<?php echo esc_attr($thumbnail_src); ?>" />
<meta itemprop="image" content="<?php echo esc_attr($thumbnail_src); ?>" />
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '507436489771545',
xfbml : true,
version : 'v3.1'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<?php
}
add_action('wp_head', 'insert_social_tags_in_head', 5);
function insert_GA_in_head() {
if (!is_user_logged_in()):
?>
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)}, i[r].l = 1 * new Date();a = s.createElement(o), m = s.getElementsByTagName(o)[0];a.async = 1;a.src = g;m.parentNode.insertBefore(a, m)})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-127396041-1', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
<?php
endif;
}
add_action('wp_head', 'insert_GA_in_head', 6);
function mxp_ajax_get_next_page_data() {
$max_num_pages = $_POST['max_num_pages'];
$current_page = $_POST['current_page'];
$found_posts = $_POST['found_posts'];
$nonce = $_POST['nonce'];
if (!wp_verify_nonce($nonce, 'mxp-ajax-nonce')) {
wp_send_json_error(array('code' => 500, 'data' => '', 'msg' => '錯誤的請求'));
}
if (!isset($max_num_pages) || $max_num_pages == "" ||
!isset($current_page) || $current_page == "" ||
!isset($found_posts) || $found_posts == "") {
wp_send_json_error(array('code' => 500, 'data' => '', 'msg' => '錯誤的請求'));
}
$ids = get_posts(array(
'fields' => 'ids', // Only get post IDs
'posts_per_page' => get_option('posts_per_page'),
'post_type' => 'post',
'paged' => intval($current_page) + 1,
));
$str = '';
foreach ($ids as $key => $id) {
$name = get_post_meta($id, 'wctp2018-author-name', true);
//$title = mb_substr(get_post_meta($id, 'wctp2018-post-title', true), 0, 20);
$content = mb_substr(get_post_meta($id, 'wctp2018-post-content', true), 0, 40) . "...";
$image_large = get_post_meta($id, 'wctp2018-post-image-large', true);
$str .= '<div class="col-md-3 m_b_20 post"><div class="box"><div class=" post_img"><a href="' . get_permalink($id) . '"><img src="' . $image_large . '"/></a></div><a href="' . get_permalink($id) . '" class="name"><h2>' . esc_html($content . ' - ' . $name) . '</h2></a></div></div>';
}
wp_send_json_success(array('code' => 200, 'data' => $str));
}
add_action('wp_ajax_nopriv_mxp_ajax_get_next_page_data', 'mxp_ajax_get_next_page_data');
function mxp_wctpe2018_form_processing() {
if (isset($_POST['mxp-postkey']) && wp_verify_nonce($_POST['mxp-postkey'], 'mxp-wctpe2018form-nonce')) {
if (!function_exists('media_handle_upload')) {
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
}
$url = parse_url($_SERVER['REQUEST_URI']);
$path = isset($url['path']) ? $url['path'] : '';
$query = isset($url['query']) ? '?' . $url['query'] : '';
//$accept_mime_type = array('image/png', 'image/jpeg', 'image/gif');
if (isset($_POST['mxp-image']) && $_POST['mxp-image'] != "" &&
isset($_POST['mxp-name']) && $_POST['mxp-name'] != "" &&
isset($_POST['mxp-email']) && $_POST['mxp-email'] != "" &&
isset($_POST['mxp-title']) && $_POST['mxp-title'] != "" &&
isset($_POST['mxp-content']) && $_POST['mxp-content'] != "") {
$name = strip_tags($_POST['mxp-name']);
$email = strip_tags($_POST['mxp-email']);
$website = isset($_POST['mxp-website']) ? strip_tags($_POST['mxp-website']) : "";
$email = filter_var($email, FILTER_VALIDATE_EMAIL) ? $email : "Anonymous";
$posttitle = strip_tags($_POST['mxp-title']);
$content = nl2br(strip_tags($_POST['mxp-content']));
setcookie('user_name', $name, time() + 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
setcookie('user_email', $email, time() + 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
setcookie('user_website', $website, time() + 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
setcookie('user_posttitle', $posttitle, time() + 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
//建立一篇文章
$now = time();
$pid = wp_insert_post(array(
'post_title' => $name . " : " . $posttitle,
'post_content' => '',
'post_name' => 'post-' . $now,
'post_excerpt' => $content,
'post_status' => "publish",
'post_author' => 1,
'post_category' => 0,
'tags_input' => array('WCTPE'),
'comment_status' => "open",
'ping_status' => "open",
'post_type' => "post",
));
add_post_meta($pid, 'wctp2018-post-datetime', $now);
add_post_meta($pid, 'wctp2018-author-email', $email);
add_post_meta($pid, 'wctpe2018-author-website', $website);
add_post_meta($pid, 'wctp2018-author-name', $name);
add_post_meta($pid, 'wctp2018-post-title', $posttitle);
add_post_meta($pid, 'wctp2018-post-content', $content);
$base64_image_string = $_POST['mxp-image'];
$splited = explode(',', substr($base64_image_string, 5), 2);
$mime = $splited[0];
$data = $splited[1];
$mime_split_without_base64 = explode(';', $mime, 2);
$mime_split = explode('/', $mime_split_without_base64[0], 2);
$extension = "";
$output_file_with_extension = "";
if (count($mime_split) == 2) {
$extension = $mime_split[1];
if ($extension == 'jpeg') {
$extension = 'jpg';
}
$output_file_with_extension = 'user-upload-' . time() . '.' . $extension;
}
if (in_array($extension, array('jpg')) && $output_file_with_extension != "") {
$tempName = tempnam(sys_get_temp_dir(), 'mxp-tw');
$tempName = realpath($tempName);
file_put_contents($tempName, base64_decode($data));
$tid = media_handle_sideload(array('name' => $output_file_with_extension, 'type' => 'image/jpeg', 'tmp_name' => $tempName, 'error' => 0, 'size' => strlen($data)), $pid, $name . " / " . $posttitle);
if (!is_wp_error($tid)) {
$src = wp_get_attachment_url($tid);
$large_thum = image_downsize($tid, 'large');
$large_thum_path = $large_thum[0];
add_post_meta($pid, 'wctp2018-post-image-full', $src);
add_post_meta($pid, 'wctp2018-post-image-large', $large_thum_path);
set_post_thumbnail($pid, $tid);
}
$update_post = array(
'ID' => $pid,
'post_content' => '[wctpe2018_display id="' . $pid . '"]',
);
wp_update_post($update_post);
wp_redirect(get_post_permalink($pid));
exit;
} else {
if ($path != '') {
wp_redirect($path . '?oops=1');
exit;
}
}
} else {
if ($path != '') {
wp_redirect($path . '?oops=2');
exit;
}
}
}
}
add_action('init', 'mxp_wctpe2018_form_processing');
function wctpe2018_search_form($form) {
$form = '<div class="searchform find_post"><form role="search" method="get" id="searchform" class="searchform" action="' . home_url('/') . '" >
<div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
<input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="Keywords / 關鍵字"/>
<input type="submit" id="searchsubmit" value="' . esc_attr__('Search') . '" />
</div>
</form>';
return $form;
}
add_filter('get_search_form', 'wctpe2018_search_form');
function wctpe2018_get_searchform_shortcode($atts) {
$form = get_search_form();
return $form;
}
add_shortcode('wctpe2018_get_searchform', 'wctpe2018_get_searchform_shortcode');
function wctpe2018_typekit() {
?>
<script>
(function(d) {
var config = {
kitId: 'uas7eyf',
scriptTimeout: 3000,
async: true
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
})(document);
</script>
<?php
}
add_action('wp_head', 'wctpe2018_typekit');