| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Gets a number of posts and displays them as options |
| 5 |
* |
| 6 |
* @param array $query_args Optional. Overrides defaults. |
| 7 |
* @param bool $force Force the pages to be loaded even if not on settings |
| 8 |
* |
| 9 |
* @see: https://github.com/WebDevStudios/CMB2/wiki/Adding-your-own-field-types |
| 10 |
* @return array An array of options that matches the CMB2 options array |
| 11 |
*/ |
| 12 |
function give_cmb2_get_post_options( $query_args, $force = false ) { |
| 13 |
|
| 14 |
$post_options = [ '' => '' ]; // Blank option |
| 15 |
|
| 16 |
if ( ( ! isset( $_GET['page'] ) || 'give-settings' != $_GET['page'] ) && ! $force ) { |
| 17 |
return $post_options; |
| 18 |
} |
| 19 |
|
| 20 |
$args = wp_parse_args( |
| 21 |
$query_args, |
| 22 |
[ |
| 23 |
'post_type' => 'page', |
| 24 |
'numberposts' => 10, |
| 25 |
] |
| 26 |
); |
| 27 |
|
| 28 |
$posts = get_posts( $args ); |
| 29 |
|
| 30 |
if ( $posts ) { |
| 31 |
foreach ( $posts as $post ) { |
| 32 |
|
| 33 |
$post_options[ $post->ID ] = $post->post_title; |
| 34 |
|
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
return $post_options; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Featured Image Sizes |
| 44 |
* |
| 45 |
* Outputs an array for the "Featured Image Size" option found under Settings > Display Options. |
| 46 |
* |
| 47 |
* @since 1.4 |
| 48 |
* |
| 49 |
* @global $_wp_additional_image_sizes |
| 50 |
* |
| 51 |
* @return array $sizes |
| 52 |
*/ |
| 53 |
function give_get_featured_image_sizes() { |
| 54 |
global $_wp_additional_image_sizes; |
| 55 |
|
| 56 |
$sizes = []; |
| 57 |
$get_sizes = get_intermediate_image_sizes(); |
| 58 |
$core_image_sizes = [ 'thumbnail', 'medium', 'medium_large', 'large' ]; |
| 59 |
|
| 60 |
// This will help us to filter special characters from a string |
| 61 |
$filter_slug_items = [ '_', '-' ]; |
| 62 |
|
| 63 |
foreach ( $get_sizes as $_size ) { |
| 64 |
|
| 65 |
// Converting image size slug to title case |
| 66 |
$sizes[ $_size ] = give_slug_to_title( $_size, $filter_slug_items ); |
| 67 |
|
| 68 |
if ( in_array( $_size, $core_image_sizes ) ) { |
| 69 |
$sizes[ $_size ] .= ' (' . get_option( "{$_size}_size_w" ) . 'x' . get_option( "{$_size}_size_h" ); |
| 70 |
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { |
| 71 |
$sizes[ $_size ] .= " ({$_wp_additional_image_sizes[ $_size ]['width']} x {$_wp_additional_image_sizes[ $_size ]['height']}"; |
| 72 |
} |
| 73 |
|
| 74 |
// Based on the above image height check, label the respective resolution as responsive |
| 75 |
if ( ( array_key_exists( $_size, $_wp_additional_image_sizes ) && ! $_wp_additional_image_sizes[ $_size ]['crop'] ) || ( in_array( $_size, $core_image_sizes ) && ! get_option( "{$_size}_crop" ) ) ) { |
| 76 |
$sizes[ $_size ] .= ' - responsive'; |
| 77 |
} |
| 78 |
|
| 79 |
$sizes[ $_size ] .= ')'; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
return apply_filters( 'give_get_featured_image_sizes', $sizes ); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Slug to Title |
| 89 |
* |
| 90 |
* Converts a string with hyphen(-) or underscores(_) or any special character to a string with Title case |
| 91 |
* |
| 92 |
* @since 1.8.8 |
| 93 |
* |
| 94 |
* @param string $string |
| 95 |
* @param array $filters |
| 96 |
* |
| 97 |
* @return string $string |
| 98 |
*/ |
| 99 |
function give_slug_to_title( $string, $filters = [] ) { |
| 100 |
|
| 101 |
foreach ( $filters as $filter_item ) { |
| 102 |
$string = str_replace( $filter_item, ' ', $string ); |
| 103 |
} |
| 104 |
|
| 105 |
// Return updated string after converting it to title case |
| 106 |
return ucwords( $string ); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Display the API Keys |
| 113 |
* |
| 114 |
* @since 1.0 |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
function give_api_callback() { |
| 118 |
|
| 119 |
if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Fires before displaying API keys. |
| 125 |
* |
| 126 |
* @since 1.0 |
| 127 |
*/ |
| 128 |
do_action( 'give_tools_api_keys_before' ); |
| 129 |
|
| 130 |
require_once GIVE_PLUGIN_DIR . 'includes/admin/class-api-keys-table.php'; |
| 131 |
|
| 132 |
$api_keys_table = new Give_API_Keys_Table(); |
| 133 |
$api_keys_table->prepare_items(); |
| 134 |
$api_keys_table->display(); |
| 135 |
?> |
| 136 |
<span class="give-metabox-description api-description"> |
| 137 |
<?php |
| 138 |
echo sprintf( |
| 139 |
/* translators: 1: http://docs.givewp.com/api 2: http://docs.givewp.com/addon-zapier */ |
| 140 |
__( 'You can create API keys for individual users within their profile edit screen. API keys allow users to use the <a href="%1$s" target="_blank">GiveWP REST API</a> to retrieve donation data in JSON or XML for external applications or devices, such as <a href="%2$s" target="_blank">Zapier</a>.', 'give' ), |
| 141 |
esc_url( 'http://docs.givewp.com/api' ), |
| 142 |
esc_url( 'http://docs.givewp.com/addon-zapier' ) |
| 143 |
); |
| 144 |
?> |
| 145 |
</span> |
| 146 |
<?php |
| 147 |
|
| 148 |
/** |
| 149 |
* Fires after displaying API keys. |
| 150 |
* |
| 151 |
* @since 1.0 |
| 152 |
*/ |
| 153 |
do_action( 'give_tools_api_keys_after' ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Hide char in string |
| 159 |
* |
| 160 |
* @param string $str |
| 161 |
* @param int $show_char_count |
| 162 |
* @param string $replace |
| 163 |
* |
| 164 |
* @return string |
| 165 |
* @since 2.5.0 |
| 166 |
*/ |
| 167 |
function give_hide_char( $str, $show_char_count, $replace = '*' ) { |
| 168 |
return str_repeat( |
| 169 |
$replace, |
| 170 |
strlen( $str ) - $show_char_count |
| 171 |
) . substr( |
| 172 |
$str, |
| 173 |
- $show_char_count, |
| 174 |
$show_char_count |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Format marKdown formatted string. |
| 181 |
* |
| 182 |
* @param string $readme Markdown format string |
| 183 |
* |
| 184 |
* @return string |
| 185 |
* @since 2.5.0 |
| 186 |
*/ |
| 187 |
function give_get_format_md( $readme ) { |
| 188 |
$readme = preg_replace( '/`(.*?)`/', '<code>\\1</code>', $readme ); |
| 189 |
$readme = preg_replace( '/[\040]\*\*(.*?)\*\*/', ' <strong>\\1</strong>', $readme ); |
| 190 |
$readme = preg_replace( '/[\040]\*(.*?)\*/', ' <em>\\1</em>', $readme ); |
| 191 |
$readme = preg_replace( '/= (.*?) =/', '<h4>\\1</h4>', $readme ); |
| 192 |
$readme = preg_replace( '/\[(.*?)\]\((.*?)\)/', '<a href="\\2">\\1</a>', $readme ); |
| 193 |
|
| 194 |
return $readme; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Add-ons Render Feed |
| 199 |
* |
| 200 |
* Renders the add-ons page feed. |
| 201 |
* |
| 202 |
* @param string $feed_type |
| 203 |
* @param bool $echo |
| 204 |
* |
| 205 |
* @return string |
| 206 |
* @since 1.0 |
| 207 |
*/ |
| 208 |
function give_add_ons_feed( $feed_type = '', $echo = true ) { |
| 209 |
|
| 210 |
$addons_debug = false; // set to true to debug. NEVER LEAVE TRUE IN PRODUCTION. |
| 211 |
$cache_key = $feed_type ? "give_add_ons_feed_{$feed_type}" : 'give_add_ons_feed'; |
| 212 |
$cache = Give_Cache::get( $cache_key, true ); |
| 213 |
$feed_url = Give_License::get_website_url() . 'downloads/feed/'; |
| 214 |
|
| 215 |
if ( false === $cache || ( true === $addons_debug && true === WP_DEBUG ) ) { |
| 216 |
switch ( $feed_type ) { |
| 217 |
case 'price-bundle': |
| 218 |
$feed_url = Give_License::get_website_url() . 'downloads/feed/addons-price-bundles.php'; |
| 219 |
break; |
| 220 |
case 'addons-directory': |
| 221 |
$feed_url = Give_License::get_website_url() . 'downloads/feed/index.php'; |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
if ( function_exists( 'vip_safe_wp_remote_get' ) ) { |
| 226 |
$feed = vip_safe_wp_remote_get( $feed_url, false, 3, 1, 20, [ 'sslverify' => false ] ); |
| 227 |
} else { |
| 228 |
$feed = wp_remote_get( $feed_url, [ 'sslverify' => false ] ); |
| 229 |
} |
| 230 |
|
| 231 |
if ( ! is_wp_error( $feed ) ) { |
| 232 |
if ( ! empty( $feed['body'] ) ) { |
| 233 |
$cache = wp_remote_retrieve_body( $feed ); |
| 234 |
Give_Cache::set( $cache_key, $cache, DAY_IN_SECONDS, true ); |
| 235 |
} |
| 236 |
} else { |
| 237 |
$cache = sprintf( |
| 238 |
'<div class="error inline"><p>%s</p></div>', |
| 239 |
esc_html__( 'There was an error retrieving the GiveWP add-ons list from the server. Please try again.', 'give' ) |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
$cache = wp_kses_post( $cache ); |
| 245 |
|
| 246 |
if ( $echo ) { |
| 247 |
echo $cache; |
| 248 |
} |
| 249 |
|
| 250 |
return $cache; |
| 251 |
} |
| 252 |
|