| 1 |
<?php // User Submitted Posts - Shortcodes misc. |
| 2 |
|
| 3 |
/* |
| 4 |
Shortcode: Reset form button |
| 5 |
Returns the markup for a reset-form button |
| 6 |
Syntax: [usp-reset-button class="aaa,bbb,ccc" value="Reset form" url="https://example.com/usp-pro/submit/"] |
| 7 |
Attributes: |
| 8 |
class = classes for the parent element (optional, default: none) |
| 9 |
value = link text (optional, default: "Reset form") |
| 10 |
url = the URL where your form is displayed (can use %%current%% for current URL) |
| 11 |
|
| 12 |
*/ |
| 13 |
function usp_reset_button_shortcode($args) { |
| 14 |
|
| 15 |
extract(shortcode_atts(array( |
| 16 |
'class' => '', |
| 17 |
'value' => __('Reset form', 'usp'), |
| 18 |
'url' => '#please-check-shortcode', |
| 19 |
), $args)); |
| 20 |
|
| 21 |
$protocol = is_ssl() ? 'https://' : 'http://'; |
| 22 |
|
| 23 |
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; |
| 24 |
|
| 25 |
$current = isset($_SERVER['REQUEST_URI']) ? $protocol . $host . $_SERVER['REQUEST_URI'] : ''; |
| 26 |
|
| 27 |
$url = preg_replace('/%%current%%/', $current, $url); |
| 28 |
|
| 29 |
$url = remove_query_arg(array('usp_reset_form', 'post_id', 'success', 'usp-error'), $url); |
| 30 |
|
| 31 |
$href = get_option('permalink_structure') ? $url .'?usp_reset_form=true"' : $url .'&usp_reset_form=true'; |
| 32 |
|
| 33 |
$class = empty($class) ? '' : ' class="'. esc_attr($class) .'"'; |
| 34 |
|
| 35 |
$output = '<p'. $class .'><a href="'. esc_url($href) .'">'. esc_html($value) .'</a></p>'; |
| 36 |
|
| 37 |
return $output; |
| 38 |
|
| 39 |
} |
| 40 |
add_shortcode('usp-reset-button', 'usp_reset_button_shortcode'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/* |
| 45 |
Displays a list of all user submitted posts |
| 46 |
Bonus: includes any posts submitted by the Pro version of USP :) |
| 47 |
Shortcode: |
| 48 |
[usp_display_posts userid="current"] : displays all submitted posts by current logged-in user |
| 49 |
[usp_display_posts userid="1"] : displays all submitted posts by registered user with ID = 1 |
| 50 |
[usp_display_posts userid="Pat Smith"] : displays all submitted posts by author name "Pat Smith" |
| 51 |
[usp_display_posts userid="all"] : displays all submitted posts by all users/authors |
| 52 |
[usp_display_posts userid="all" numposts="5"] : limit to 5 posts |
| 53 |
|
| 54 |
Note that the Pro version of USP provides many more options for the display-posts shortcode: |
| 55 |
|
| 56 |
https://plugin-planet.com/usp-pro-display-list-submitted-posts/ |
| 57 |
|
| 58 |
*/ |
| 59 |
function usp_display_posts($attr = array(), $content = null) { |
| 60 |
|
| 61 |
global $post; |
| 62 |
|
| 63 |
extract(shortcode_atts(array( |
| 64 |
|
| 65 |
'userid' => 'all', |
| 66 |
'post_type' => 'post', |
| 67 |
'numposts' => -1, |
| 68 |
|
| 69 |
), $attr)); |
| 70 |
|
| 71 |
if (ctype_digit($userid)) { |
| 72 |
|
| 73 |
$args = array( |
| 74 |
'author' => $userid, |
| 75 |
'posts_per_page' => $numposts, |
| 76 |
'post_type' => $post_type, |
| 77 |
'meta_key' => 'is_submission', |
| 78 |
'meta_value' => '1' |
| 79 |
); |
| 80 |
|
| 81 |
} elseif ($userid === 'all') { |
| 82 |
|
| 83 |
$args = array( |
| 84 |
'posts_per_page' => $numposts, |
| 85 |
'post_type' => $post_type, |
| 86 |
'meta_key' => 'is_submission', |
| 87 |
'meta_value' => '1' |
| 88 |
); |
| 89 |
|
| 90 |
} elseif ($userid === 'current') { |
| 91 |
|
| 92 |
$args = array( |
| 93 |
'author' => get_current_user_id(), |
| 94 |
'posts_per_page' => $numposts, |
| 95 |
'post_type' => $post_type, |
| 96 |
'meta_key' => 'is_submission', |
| 97 |
'meta_value' => '1' |
| 98 |
); |
| 99 |
|
| 100 |
} else { |
| 101 |
|
| 102 |
$args = array( |
| 103 |
'posts_per_page' => $numposts, |
| 104 |
'post_type' => $post_type, |
| 105 |
|
| 106 |
'meta_query' => array( |
| 107 |
|
| 108 |
'relation' => 'AND', |
| 109 |
|
| 110 |
array( |
| 111 |
'key' => 'is_submission', |
| 112 |
'value' => '1' |
| 113 |
), |
| 114 |
array( |
| 115 |
'key' => 'user_submit_name', |
| 116 |
'value' => $userid |
| 117 |
) |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
$args = apply_filters('usp_display_posts_args', $args); |
| 124 |
|
| 125 |
$submitted_posts = get_posts($args); |
| 126 |
|
| 127 |
$display_posts = '<ul>'; |
| 128 |
|
| 129 |
foreach ($submitted_posts as $post) { |
| 130 |
|
| 131 |
setup_postdata($post); |
| 132 |
|
| 133 |
$display_posts .= '<li><a href="'. get_the_permalink() .'" title="'. esc_attr__('View full post', 'usp') .'">'. get_the_title() .'</a></li>'; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
$display_posts .= '</ul>'; |
| 138 |
|
| 139 |
wp_reset_postdata(); |
| 140 |
|
| 141 |
return $display_posts; |
| 142 |
|
| 143 |
} |
| 144 |
add_shortcode('usp_display_posts', 'usp_display_posts'); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
/* |
| 149 |
Shortcode: [usp_gallery] |
| 150 |
Displays a gallery of submitted images for the current post |
| 151 |
Syntax: [usp_gallery size="" format="" target="" class="" number="" id=""] |
| 152 |
Notes: See usp_get_images() for inline notes and more infos |
| 153 |
*/ |
| 154 |
if (!function_exists('usp_gallery')) : |
| 155 |
|
| 156 |
function usp_gallery($attr, $content = null) { |
| 157 |
|
| 158 |
extract(shortcode_atts(array( |
| 159 |
|
| 160 |
'size' => 'thumbnail', |
| 161 |
'format' => 'image', |
| 162 |
'target' => 'blank', |
| 163 |
'class' => '', |
| 164 |
'number' => 100, |
| 165 |
'id' => false, |
| 166 |
|
| 167 |
), $attr)); |
| 168 |
|
| 169 |
$images = usp_get_images($size, $format, $target, $class, $number, $id); |
| 170 |
|
| 171 |
$gallery = ''; |
| 172 |
|
| 173 |
foreach ($images as $image) $gallery .= $image; |
| 174 |
|
| 175 |
$gallery = $gallery ? '<div class="usp-image-gallery">'. $gallery .'</div>' : ''; |
| 176 |
|
| 177 |
return $gallery; |
| 178 |
|
| 179 |
} |
| 180 |
add_shortcode('usp_gallery', 'usp_gallery'); |
| 181 |
|
| 182 |
endif; |