· 10 years ago · Sep 22, 2016, 12:48 PM
1<?php
2/**
3 * Beetroot functions and definitions.
4 *
5 * @link https://developer.wordpress.org/themes/basics/theme-functions/
6 */
7
8// # Load modules
9
10// Clean up theme
11require_once __DIR__ . '/lib/cleanup.php';
12
13// Enqueue styles and scripts
14require_once __DIR__ . '/lib/enqueue-scripts.php';
15
16// Implement the Custom Header feature.
17require_once __DIR__ . '/lib/framework.php';
18
19// Load theme support options
20require_once __DIR__ . '/lib/theme-support.php';
21
22// Custom template tags for this theme.
23require_once __DIR__ . '/lib/template-tags.php';
24
25// Menu areas
26require_once __DIR__ . '/lib/menu-areas.php';
27
28// Widget areas
29require_once __DIR__ . '/lib/widget-areas.php';
30
31// Load Visual Composer shortcodes
32require_once __DIR__ . '/lib/vc_shortcodes.php';
33
34// Load Jetpack compatibility file.
35require_once __DIR__ . '/lib/jetpack.php';
36
37// Rewrite rules
38require_once __DIR__ . '/lib/rewrite-rules.php';
39
40// AJAX Handlers
41require_once __DIR__ . '/lib/ajax-handlers.php';
42
43
44// Theme the TinyMCE editor
45// You should create custom-editor-style.css in your theme folder
46add_editor_style('custom-editor-style.css');
47
48
49// Custom CSS for the login page
50// Create wp-login.css in your theme folder
51function loginCSS()
52{
53 echo '<link rel="stylesheet" type="text/css" href="' . get_template_directory_uri('beetroot') . '/wp-login.css"/>';
54}
55
56add_action('login_head', 'loginCSS');
57
58
59function wp_has_sidebar($classes)
60{
61 if (is_active_sidebar('sidebar')) {
62 // add 'class-name' to the $classes array
63 $classes[] = 'has_sidebar';
64 }
65 // return the $classes array
66 return $classes;
67}
68
69add_filter('body_class', 'wp_has_sidebar');
70
71
72// Remove the version number of WP
73// Warning - this info is also available in the readme.html file in your root directory - delete this file!
74remove_action('wp_head', 'wp_generator');
75
76
77// Obscure login screen error messages
78function wp_login_obscure()
79{
80 return '<strong>Error</strong>: wrong username or password';
81}
82
83add_filter('login_errors', 'wp_login_obscure');
84
85
86// Disable the theme / plugin text editor in Admin
87define('DISALLOW_FILE_EDIT', true);
88
89//ACF Options
90if (function_exists('acf_add_options_page')) {
91 acf_add_options_page('Theme Settings');
92}
93
94//WPML Language selector
95function languages_list()
96{
97 $languages = icl_get_languages('skip_missing=0&orderby=code');
98 if (!empty($languages)) {
99 echo '<ul id="language_list" class="list-inline navbar-right">';
100 foreach ($languages as $l) {
101 echo '<li>';
102 if ($l['country_flag_url']) {
103 if (!$l['active']) echo '<a href="' . $l['url'] . '">';
104 echo $l['language_code'];
105 if (!$l['active']) echo '</a>';
106 }
107 echo '</li>';
108 }
109 echo '</ul>';
110 }
111}
112
113/* Thumbnail upscale */
114function alx_thumbnail_upscale($default, $orig_w, $orig_h, $new_w, $new_h, $crop)
115{
116 if (!$crop) return null; // let the wordpress default function handle this
117
118 $aspect_ratio = $orig_w / $orig_h;
119 $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
120
121 $crop_w = round($new_w / $size_ratio);
122 $crop_h = round($new_h / $size_ratio);
123
124 $s_x = floor(($orig_w - $crop_w) / 2);
125 $s_y = floor(($orig_h - $crop_h) / 2);
126
127 return array(0, 0, (int)$s_x, (int)$s_y, (int)$new_w, (int)$new_h, (int)$crop_w, (int)$crop_h);
128}
129
130add_filter('image_resize_dimensions', 'alx_thumbnail_upscale', 10, 6);
131
132// Image sizes
133add_image_size('homepage-post-thumbnail', 215, 145, true);
134add_image_size('homepage-exhibition-thumbnail', 330, 280, true);
135add_image_size('footer-popular-artwork', 214, 188, true);
136add_image_size('blog-list-post-thumbnail', 883, 400, true);
137
138//Limiting Access to WordPress Wp-admin
139//add_action('init', 'blockusers_init');
140//function blockusers_init()
141//{
142// if (is_admin() && !current_user_can('administrator') &&
143// !(defined('DOING_AJAX') && DOING_AJAX)
144// ) {
145// wp_redirect(home_url());
146// exit;
147// }
148//}
149
150/* Disable WordPress Admin Bar for all users but admins. */
151//add_filter('show_admin_bar', '__return_false');
152//redirect WP Logout page to Homepage
153//add_action('wp_logout', 'go_home');
154//function go_home()
155//{
156// wp_redirect(home_url());
157// exit();
158//}
159//
160//add_filter( 'wp_nav_menu_items', 'wti_loginout_menu_link', 10, 2 );
161
162function wti_loginout_menu_link($items, $args)
163{
164 if ($args->theme_location == 'right_header_menu') {
165 if (is_user_logged_in()) {
166 $items .= '<li class="right"><a href="' . wp_logout_url() . '">' . __("Log Out") . '</a></li>';
167 }
168 }
169 return $items;
170}
171
172//------------------------------- Woocommerce
173//Remove add to cart (archive)
174function remove_loop_button()
175{
176 remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
177}
178
179add_action('init', 'remove_loop_button');
180
181//Like Function
182function update_like_count()
183{
184
185 $post_id = intval($_GET['post_id']);
186
187 if (filter_var($post_id, FILTER_VALIDATE_INT)) {
188
189 $article = get_post($post_id);
190 $output_count = 0;
191
192 if (!is_null($article)) {
193 $count = get_post_meta($post_id, 'my-post-likes', true);
194 if ($count == '') {
195 update_post_meta($post_id, 'my-post-likes', '1');
196 $output_count = 1;
197 } else {
198 $n = intval($count);
199 $n++;
200 update_post_meta($post_id, 'my-post-likes', $n);
201 $output_count = $n;
202 }
203 }
204
205 }
206 $output = array('count' => $output_count);
207 echo json_encode($output);
208 exit();
209}
210
211add_action('wp_ajax_my_update_likes', 'update_like_count');
212add_action('wp_ajax_nopriv_my_update_likes', 'update_like_count');
213
214function display_post_likes($post_id = null)
215{
216 $value = '';
217 if (is_null($post_id)) {
218 global $post;
219 $value = get_post_meta($post->ID, 'my-post-likes', true);
220
221 } else {
222 $value = get_post_meta($post_id, 'my-post-likes', true);
223 }
224
225 echo $value;
226}
227
228
229/**
230 * Register the stylesheets for the public-facing side of the site.
231 * @since 0.5
232 */
233add_action('wp_enqueue_scripts', 'sl_enqueue_scripts');
234function sl_enqueue_scripts()
235{
236 wp_enqueue_script('simple-likes-public-js', get_template_directory_uri() . '/assets/src/javascript/simple-likes-public.js', array('jquery'), '0.5', false);
237 wp_localize_script('simple-likes-public-js', 'simpleLikes', array(
238 'ajaxurl' => admin_url('admin-ajax.php'),
239 'like' => __('Like', 'YourThemeTextDomain'),
240 'unlike' => __('Unlike', 'YourThemeTextDomain')
241 ));
242}
243
244/**
245 * Processes like/unlike
246 * @since 0.5
247 */
248add_action('wp_ajax_nopriv_process_simple_like', 'process_simple_like');
249add_action('wp_ajax_process_simple_like', 'process_simple_like');
250function process_simple_like()
251{
252 // Security
253 $nonce = isset($_REQUEST['nonce']) ? sanitize_text_field($_REQUEST['nonce']) : 0;
254 if (!wp_verify_nonce($nonce, 'simple-likes-nonce')) {
255 exit(__('Not permitted', 'YourThemeTextDomain'));
256 }
257 // Test if javascript is disabled
258 $disabled = (isset($_REQUEST['disabled']) && $_REQUEST['disabled'] == true) ? true : false;
259 // Test if this is a comment
260 $is_comment = (isset($_REQUEST['is_comment']) && $_REQUEST['is_comment'] == 1) ? 1 : 0;
261 // Base variables
262 $post_id = (isset($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) ? $_REQUEST['post_id'] : '';
263 $result = array();
264 $post_users = NULL;
265 $like_count = 0;
266 // Get plugin options
267 if ($post_id != '') {
268 $count = ($is_comment == 1) ? get_comment_meta($post_id, "_comment_like_count", true) : get_post_meta($post_id, "_post_like_count", true); // like count
269 $count = (isset($count) && is_numeric($count)) ? $count : 0;
270 if (!already_liked($post_id, $is_comment)) { // Like the post
271 if (is_user_logged_in()) { // user is logged in
272 $user_id = get_current_user_id();
273 $post_users = post_user_likes($user_id, $post_id, $is_comment);
274 if ($is_comment == 1) {
275 // Update User & Comment
276 $user_like_count = get_user_option("_comment_like_count", $user_id);
277 $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
278 update_user_option($user_id, "_comment_like_count", ++$user_like_count);
279 if ($post_users) {
280 update_comment_meta($post_id, "_user_comment_liked", $post_users);
281 }
282 } else {
283 // Update User & Post
284 $user_like_count = get_user_option("_user_like_count", $user_id);
285 $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
286 update_user_option($user_id, "_user_like_count", ++$user_like_count);
287 if ($post_users) {
288 update_post_meta($post_id, "_user_liked", $post_users);
289 }
290 }
291 } else { // user is anonymous
292 $user_ip = sl_get_ip();
293 $post_users = post_ip_likes($user_ip, $post_id, $is_comment);
294 // Update Post
295 if ($post_users) {
296 if ($is_comment == 1) {
297 update_comment_meta($post_id, "_user_comment_IP", $post_users);
298 } else {
299 update_post_meta($post_id, "_user_IP", $post_users);
300 }
301 }
302 }
303 $like_count = ++$count;
304 $response['status'] = "liked";
305 $response['icon'] = get_liked_icon();
306 } else { // Unlike the post
307 if (is_user_logged_in()) { // user is logged in
308 $user_id = get_current_user_id();
309 $post_users = post_user_likes($user_id, $post_id, $is_comment);
310 // Update User
311 if ($is_comment == 1) {
312 $user_like_count = get_user_option("_comment_like_count", $user_id);
313 $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
314 if ($user_like_count > 0) {
315 update_user_option($user_id, "_comment_like_count", --$user_like_count);
316 }
317 } else {
318 $user_like_count = get_user_option("_user_like_count", $user_id);
319 $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
320 if ($user_like_count > 0) {
321 update_user_option($user_id, '_user_like_count', --$user_like_count);
322 }
323 }
324 // Update Post
325 if ($post_users) {
326 $uid_key = array_search($user_id, $post_users);
327 unset($post_users[$uid_key]);
328 if ($is_comment == 1) {
329 update_comment_meta($post_id, "_user_comment_liked", $post_users);
330 } else {
331 update_post_meta($post_id, "_user_liked", $post_users);
332 }
333 }
334 } else { // user is anonymous
335 $user_ip = sl_get_ip();
336 $post_users = post_ip_likes($user_ip, $post_id, $is_comment);
337 // Update Post
338 if ($post_users) {
339 $uip_key = array_search($user_ip, $post_users);
340 unset($post_users[$uip_key]);
341 if ($is_comment == 1) {
342 update_comment_meta($post_id, "_user_comment_IP", $post_users);
343 } else {
344 update_post_meta($post_id, "_user_IP", $post_users);
345 }
346 }
347 }
348 $like_count = ($count > 0) ? --$count : 0; // Prevent negative number
349 $response['status'] = "unliked";
350 $response['icon'] = get_unliked_icon();
351 }
352 if ($is_comment == 1) {
353 update_comment_meta($post_id, "_comment_like_count", $like_count);
354 update_comment_meta($post_id, "_comment_like_modified", date('Y-m-d H:i:s'));
355 } else {
356 update_post_meta($post_id, "_post_like_count", $like_count);
357 update_post_meta($post_id, "_post_like_modified", date('Y-m-d H:i:s'));
358 }
359 $response['count'] = get_like_count($like_count);
360 $response['testing'] = $is_comment;
361 if ($disabled == true) {
362 if ($is_comment == 1) {
363 wp_redirect(get_permalink(get_the_ID()));
364 exit();
365 } else {
366 wp_redirect(get_permalink($post_id));
367 exit();
368 }
369 } else {
370 wp_send_json($response);
371 }
372 }
373}
374
375/**
376 * Utility to test if the post is already liked
377 * @since 0.5
378 */
379function already_liked($post_id, $is_comment)
380{
381 $post_users = NULL;
382 $user_id = NULL;
383 if (is_user_logged_in()) { // user is logged in
384 $user_id = get_current_user_id();
385 $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_liked") : get_post_meta($post_id, "_user_liked");
386 if (count($post_meta_users) != 0) {
387 $post_users = $post_meta_users[0];
388 }
389 } else { // user is anonymous
390 $user_id = sl_get_ip();
391 $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_IP") : get_post_meta($post_id, "_user_IP");
392 if (count($post_meta_users) != 0) { // meta exists, set up values
393 $post_users = $post_meta_users[0];
394 }
395 }
396 if (is_array($post_users) && in_array($user_id, $post_users)) {
397 return true;
398 } else {
399 return false;
400 }
401} // already_liked()
402
403/**
404 * Output the like button
405 * @since 0.5
406 */
407function get_simple_likes_button($post_id, $is_comment = NULL)
408{
409 $is_comment = (NULL == $is_comment) ? 0 : 1;
410 $output = '';
411 $nonce = wp_create_nonce('simple-likes-nonce'); // Security
412 if ($is_comment == 1) {
413 $post_id_class = esc_attr(' sl-comment-button-' . $post_id);
414 $comment_class = esc_attr(' sl-comment');
415 $like_count = get_comment_meta($post_id, "_comment_like_count", true);
416 $like_count = (isset($like_count) && is_numeric($like_count)) ? $like_count : 0;
417 } else {
418 $post_id_class = esc_attr(' sl-button-' . $post_id);
419 $comment_class = esc_attr('');
420 $like_count = get_post_meta($post_id, "_post_like_count", true);
421 $like_count = (isset($like_count) && is_numeric($like_count)) ? $like_count : 0;
422 }
423 $count = get_like_count($like_count);
424 $icon_empty = get_unliked_icon();
425 $icon_full = get_liked_icon();
426 // Loader
427 $loader = '<span id="sl-loader"></span>';
428 // Liked/Unliked Variables
429 if (already_liked($post_id, $is_comment)) {
430 $class = esc_attr(' liked');
431 $title = __('Unlike', 'YourThemeTextDomain');
432 $icon = $icon_full;
433 } else {
434 $class = '';
435 $title = __('Like', 'YourThemeTextDomain');
436 $icon = $icon_empty;
437 }
438 $output = '<span class="sl-wrapper"><a href="' . admin_url('admin-ajax.php?action=process_simple_like' . '&post_id=' . $post_id . '&nonce=' . $nonce . '&is_comment=' . $is_comment . '&disabled=true') . '" class="sl-button' . $post_id_class . $class . $comment_class . '" data-nonce="' . $nonce . '" data-post-id="' . $post_id . '" data-iscomment="' . $is_comment . '" title="' . $title . '">' . $icon . $count . '</a>' . $loader . '</span>';
439 return $output;
440} // get_simple_likes_button()
441
442/**
443 * Processes shortcode to manually add the button to posts
444 * @since 0.5
445 */
446add_shortcode('jmliker', 'sl_shortcode');
447function sl_shortcode()
448{
449 return get_simple_likes_button(get_the_ID(), 0);
450} // shortcode()
451
452/**
453 * Utility retrieves post meta user likes (user id array),
454 * then adds new user id to retrieved array
455 * @since 0.5
456 */
457function post_user_likes($user_id, $post_id, $is_comment)
458{
459 $post_users = '';
460 $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_liked") : get_post_meta($post_id, "_user_liked");
461 if (count($post_meta_users) != 0) {
462 $post_users = $post_meta_users[0];
463 }
464 if (!is_array($post_users)) {
465 $post_users = array();
466 }
467 if (!in_array($user_id, $post_users)) {
468 $post_users['user-' . $user_id] = $user_id;
469 }
470 return $post_users;
471} // post_user_likes()
472
473/**
474 * Utility retrieves post meta ip likes (ip array),
475 * then adds new ip to retrieved array
476 * @since 0.5
477 */
478function post_ip_likes($user_ip, $post_id, $is_comment)
479{
480 $post_users = '';
481 $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_IP") : get_post_meta($post_id, "_user_IP");
482 // Retrieve post information
483 if (count($post_meta_users) != 0) {
484 $post_users = $post_meta_users[0];
485 }
486 if (!is_array($post_users)) {
487 $post_users = array();
488 }
489 if (!in_array($user_ip, $post_users)) {
490 $post_users['ip-' . $user_ip] = $user_ip;
491 }
492 return $post_users;
493} // post_ip_likes()
494
495/**
496 * Utility to retrieve IP address
497 * @since 0.5
498 */
499function sl_get_ip()
500{
501 if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
502 $ip = $_SERVER['HTTP_CLIENT_IP'];
503 } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
504 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
505 } else {
506 $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
507 }
508 $ip = filter_var($ip, FILTER_VALIDATE_IP);
509 $ip = ($ip === false) ? '0.0.0.0' : $ip;
510 return $ip;
511} // sl_get_ip()
512
513/**
514 * Utility returns the button icon for "like" action
515 * @since 0.5
516 */
517function get_liked_icon()
518{
519 /* If already using Font Awesome with your theme, replace svg with: <i class="fa fa-heart"></i> */
520 $icon = '<span class="sl-icon"><svg role="img" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0" y="0" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve"><path id="heart-full" d="M124 20.4C111.5-7 73.7-4.8 64 19 54.3-4.9 16.5-7 4 20.4c-14.7 32.3 19.4 63 60 107.1C104.6 83.4 138.7 52.7 124 20.4z"/>♥</svg></span>';
521 return $icon;
522} // get_liked_icon()
523
524/**
525 * Utility returns the button icon for "unlike" action
526 * @since 0.5
527 */
528function get_unliked_icon()
529{
530 /* If already using Font Awesome with your theme, replace svg with: <i class="fa fa-heart-o"></i> */
531 $icon = '<span class="sl-icon"><svg role="img" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0" y="0" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve"><path id="heart" d="M64 127.5C17.1 79.9 3.9 62.3 1 44.4c-3.5-22 12.2-43.9 36.7-43.9 10.5 0 20 4.2 26.4 11.2 6.3-7 15.9-11.2 26.4-11.2 24.3 0 40.2 21.8 36.7 43.9C124.2 62 111.9 78.9 64 127.5zM37.6 13.4c-9.9 0-18.2 5.2-22.3 13.8C5 49.5 28.4 72 64 109.2c35.7-37.3 59-59.8 48.6-82 -4.1-8.7-12.4-13.8-22.3-13.8 -15.9 0-22.7 13-26.4 19.2C60.6 26.8 54.4 13.4 37.6 13.4z"/>♥</svg></span>';
532 return $icon;
533} // get_unliked_icon()
534
535/**
536 * Utility function to format the button count,
537 * appending "K" if one thousand or greater,
538 * "M" if one million or greater,
539 * and "B" if one billion or greater (unlikely).
540 * $precision = how many decimal points to display (1.25K)
541 * @since 0.5
542 */
543function sl_format_count($number)
544{
545 $precision = 2;
546 if ($number >= 1000 && $number < 1000000) {
547 $formatted = number_format($number / 1000, $precision) . 'K';
548 } else if ($number >= 1000000 && $number < 1000000000) {
549 $formatted = number_format($number / 1000000, $precision) . 'M';
550 } else if ($number >= 1000000000) {
551 $formatted = number_format($number / 1000000000, $precision) . 'B';
552 } else {
553 $formatted = $number; // Number is less than 1000
554 }
555 $formatted = str_replace('.00', '', $formatted);
556 return $formatted;
557} // sl_format_count()
558
559/**
560 * Utility retrieves count plus count options,
561 * returns appropriate format based on options
562 * @since 0.5
563 */
564function get_like_count($like_count)
565{
566 $like_text = __('Like', 'YourThemeTextDomain');
567 if (is_numeric($like_count) && $like_count > 0) {
568 $number = sl_format_count($like_count);
569 } else {
570 $number = $like_text;
571 }
572 $count = '<span class="sl-count">' . $number . '</span>';
573 return $count;
574} // get_like_count()
575
576// User Profile List1
577add_action('show_user_profile', 'show_user_likes');
578add_action('edit_user_profile', 'show_user_likes');
579function show_user_likes($user)
580{ ?>
581<!-- <table class="form-table">-->
582<!-- <tr>-->
583<!-- <th><label for="user_likes">--><?php //_e('You Like:', 'YourThemeTextDomain'); ?><!--</label></th>-->
584<!-- <td>-->
585 <?php
586 $types = get_post_types(array('public' => true));
587 $args = array(
588 'posts_per_page' => -1,
589 'post_type' => $types,
590 'meta_query' => array(
591 array(
592 'key' => '_user_liked',
593 'value' => $user->ID,
594 'compare' => 'LIKE'
595 )
596 ));
597 $sep = '';
598 $like_query = new WP_Query($args);
599 // var_dump($like_query->posts);
600 if ($like_query->have_posts()) : ?>
601 <div class="grid">
602 <div class="grid-sizer"></div>
603 <?php while ($like_query->have_posts()) : $like_query->the_post();
604 echo $sep; ?>
605 <div class="grid-item">
606 <div class="artwork-item">
607 <a href="<?php the_permalink(); ?>">
608 <?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
609 </a>
610 <?php
611 global $authordata;
612 if (in_array('artist', $authordata->roles)) { ?>
613 <h5><?= $authordata->display_name; ?></h5>
614 <?php } ?>
615
616 <a class="prod_title" href="<?php the_permalink(); ?>">
617 <?php the_title(); ?>
618 </a>
619 <?php $height = get_field('_height');
620 $width = get_field('_width');
621 $style_field = get_field('style');
622 $comma = ',';
623 if ($style_field): ?>
624 <span><?php echo $style_field->name; echo $comma?></span>
625 <?php endif; ?>
626 <?php if($height && $width): ?>
627 <span><?php echo $height; ?></span> <span>x</span> <span><?php echo $width; ?></span> <span>cm</span>
628 <?php endif; ?>
629 <?php global $product; ?>
630 <?php if ( $price_html = $product->get_price_html() ) : ?>
631 <?php echo $price_html; ?>
632 <?php endif; ?>
633
634 <?php if (in_array('gallery', $authordata->roles)) { ?>
635 <h5><?= $authordata->display_name; ?></h5>
636 <?php } ?>
637 </div>
638 </div>
639 <?php
640 $sep = ' · ';
641 endwhile;
642 ?>
643 </div>
644 <?php else : ?>
645 <p><?php _e('You do not like anything yet.', 'YourThemeTextDomain'); ?></p>
646 <?php
647 endif;
648 wp_reset_postdata();
649 ?>
650<!-- </td>-->
651<!-- </tr>-->
652<!-- </table>-->
653<?php } // show_user_likes()
654
655function my_post_object_query($args)
656{
657 $args['author'] = get_current_user_id();
658
659 return $args;
660}
661add_filter('acf/fields/post_object/query/name=associated_artworks', 'my_post_object_query', 10, 3);
662
663function my_post_object_query2($args)
664{
665 $args['author'] = get_current_user_id();
666
667 return $args;
668}
669add_filter('acf/fields/post_object/query/name=main_artwork', 'my_post_object_query2', 10, 3);
670
671
672/**
673 * Add the input field to the form
674 *
675 * @param int $form_id
676 * @param null|int $post_id
677 * @param array $form_settings
678 */
679function your_function_name($form_id, $post_id, $form_settings)
680{
681 $value = '';
682
683 if ($post_id) {
684 $value = get_post_meta($post_id, 'ex_date', true);
685 }
686 ?>
687
688 <div class="wpuf-label">
689 <label>A demo field</label>
690 </div>
691
692 <div class="wpuf-fields">
693 <input type="text" name="my_custom_field" value="<?php echo esc_attr($value); ?>">
694 </div>
695 <?php
696}
697
698add_action('my_brand_new_hook', 'your_function_name', 10, 3);
699
700/**
701 * Update the custom field when the form submits
702 *
703 * @param type $post_id
704 */
705function update_my_brand_new_hook($post_id)
706{
707 if (isset($_POST['my_custom_field'])) {
708 update_post_meta($post_id, 'ex_date', $_POST['my_custom_field']);
709 }
710}
711
712add_action('wpuf_add_post_after_insert', 'update_my_brand_new_hook');
713add_action('wpuf_edit_post_after_update', 'update_my_brand_new_hook');
714
715
716function get_purchased_products_ids_by_author($id_user)
717{
718 global $wpdb;
719 $table_items = $wpdb->prefix . 'woocommerce_order_items';
720 $table_items_meta = $wpdb->prefix . 'woocommerce_order_itemmeta';
721
722 $products_query = "
723 SELECT DISTINCT items_meta.meta_value FROM " . $table_items_meta . " items_meta
724 INNER JOIN " . $table_items . " items ON items_meta.order_item_id = items.order_item_id
725 INNER JOIN " . $wpdb->posts . " posts ON items.order_id = posts.ID
726 WHERE posts.post_author = " . $id_user . "
727 AND items_meta.meta_key = '_product_id'
728 ";
729 $products = $wpdb->get_results($products_query);
730
731 $product_ids = array();
732
733 if (!empty($products)) {
734
735 foreach ($products as $product) {
736
737 $product_ids[] = $product->meta_value;
738
739 }
740
741 }
742
743 return $product_ids;
744
745}
746
747// Create the frontend form
748add_filter('acf/pre_save_post', 'jivedig_newdownload_pre_save_post');
749function jivedig_newdownload_pre_save_post($post_id)
750{
751
752 // check if this is to be a new post
753 if ($post_id != 'new') {
754 return $post_id;
755 }
756
757 // Create a new post
758 $post = array(
759 'post_status' => 'publish',
760 'post_title' => $_POST['acf']['field_57c00da3d380d'], // beat_title
761 'post_type' => 'exhibition',
762 );
763
764 // insert the post
765 $post_id = wp_insert_post($post);
766
767 // update $_POST['return']
768 $_POST['return'] = add_query_arg(array('post_id' => $post_id), $_POST['return']);
769
770 // return the new ID
771 return $post_id;
772
773}
774
775//ACF Title
776add_action('acf/save_post', 'custom_acf_save_exhibition', 20);
777function custom_acf_save_exhibition($post_id)
778{
779 if (is_page_template('templates/tpl-dashboard-post-exhibition.php')):
780 // bail early if no ACF data
781 if (empty($_POST['acf'])) {
782
783 return;
784 }
785
786 $data['ID'] = $post_id;
787 // Combine first name and last name to create a title
788 $title = trim($_POST['acf']['field_57c00da3d380d']);
789 $data['post_title'] = $title;
790 $data['post_name'] = sanitize_title($title);
791
792 wp_update_post($data);
793 endif;
794
795}
796
797add_action('acf/save_post', 'custom_acf_save_artwork', 20);
798function custom_acf_save_artwork($post_id)
799{
800 if (is_page_template('templates/tpl-dashboard-post-artwork.php')):
801 // bail early if no ACF data
802 if (empty($_POST['acf'])) {
803 return;
804 }
805
806 $data['ID'] = $post_id;
807 // Combine first name and last name to create a title
808 $title = trim($_POST['acf']['field_57c022c0c2d96']);
809 $data['post_title'] = $title;
810 $data['post_name'] = sanitize_title($title);
811
812 wp_update_post($data);
813 endif;
814}
815
816function acf_set_featured_image($value, $post_id, $field)
817{
818 $id = $value;
819 if (!is_numeric($id)) {
820 $data = json_decode(stripcslashes($id), true);
821 $id = $data['cropped_image'];
822 }
823 update_post_meta($post_id, '_thumbnail_id', $id);
824 return $value;
825}
826
827// acf/update_value/name={$field_name} - filter for a specific field based on it's name
828add_filter('acf/update_value/name=upload_artwork', 'acf_set_featured_image', 10, 3);
829
830//ACF Update user meta
831add_action('acf/save_post', 'example_save_temporary_user_data', 20);
832function example_save_temporary_user_data($post_id)
833{
834 // The approval should be on the back end only;
835 if (!is_admin())
836 return;
837
838 // $post_id contains "user_99" so $user_id should "99"
839 $user_id = str_replace('user_', '', $post_id);
840
841 //get the temporary data
842 $first_name = get_field('first_name', $post_id);
843 $last_name = get_field('last_name', $post_id);
844 $email = get_field('email', $post_id);
845 $billing_phone = get_field('billing_phone', $post_id);
846 $description = get_field('description', $post_id);
847 $website = get_field('url', $post_id);
848
849
850 // update the user data (in this case the first name)
851 update_user_meta($user_id, 'first_name', $first_name);
852 update_user_meta($user_id, 'last_name', $last_name);
853 update_user_meta($user_id, 'email', $email);
854 update_user_meta($user_id, 'billing_phone', $billing_phone);
855 update_user_meta($user_id, 'description', $description);
856 update_user_meta($user_id, 'url', $website);
857}
858
859//ACF Update user password
860add_filter('acf/pre_save_post', 'my_update_password');
861function my_update_password($post_id) {
862 if (substr($post_id, 0 , 5) != 'user_') {
863 // not a user
864 return $post_id;
865 }
866
867 $new_password = $_POST['acf']['field_57d0066572046']; // update this
868 $who = str_replace('user_', '', $post_id);
869
870 wp_set_password( $new_password, $who );
871 wp_clear_auth_cookie();
872 wp_set_current_user ( $who );
873 wp_set_auth_cookie ( $who );
874}
875
876//Woocommerce custom price (ACF)
877add_filter('woocommerce_get_price', function ($price, $product) {
878 return get_post_meta($product->id, '_regular_price', true);
879// return $price;
880}, 10, 2);
881
882//Woocommerce Remove Related Products Output
883function wc_remove_related_products($args)
884{
885 return array();
886}
887
888add_filter('woocommerce_related_products_args', 'wc_remove_related_products', 10);
889
890
891//Woocommerce Remove Product Information Tabs
892add_filter('woocommerce_product_tabs', 'woo_remove_product_tabs', 98);
893function woo_remove_product_tabs($tabs)
894{
895
896 unset($tabs['description']); // Remove the description tab
897 unset($tabs['reviews']); // Remove the reviews tab
898 unset($tabs['additional_information']); // Remove the additional information tab
899
900 return $tabs;
901
902}
903
904//Remove Add to cart from single product
905remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
906remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
907//Remove Product's meta
908remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);