add-ons
4 years ago
donors
4 years ago
emails
3 years ago
forms
3 years ago
payments
3 years ago
reports
4 years ago
settings
2 years ago
shortcodes
4 years ago
tools
2 years ago
upgrades
3 years ago
views
3 years ago
abstract-admin-settings-page.php
6 years ago
admin-actions.php
3 years ago
admin-filters.php
3 years ago
admin-footer.php
5 years ago
admin-pages.php
3 years ago
class-addon-activation-banner.php
4 years ago
class-admin-settings.php
4 years ago
class-api-keys-table.php
4 years ago
class-blank-slate.php
3 years ago
class-give-admin.php
5 years ago
class-give-html-elements.php
6 years ago
class-i18n-module.php
4 years ago
dashboard-widgets.php
3 years ago
give-metabox-functions.php
3 years ago
import-functions.php
3 years ago
misc-functions.php
3 years ago
plugins.php
3 years ago
setting-page-functions.php
6 years ago
misc-functions.php
348 lines
| 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 | |
| 253 | /** |
| 254 | * Handle installation and connection for SendWP via ajax |
| 255 | * |
| 256 | * @since 2.9.15 |
| 257 | */ |
| 258 | function give_sendwp_remote_install_handler () { |
| 259 | |
| 260 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 261 | wp_send_json_error( array( |
| 262 | 'error' => __( 'You do not have permission to do this.', 'give' ) |
| 263 | ) ); |
| 264 | } |
| 265 | |
| 266 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 267 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 268 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 269 | |
| 270 | $plugins = get_plugins(); |
| 271 | |
| 272 | if( ! array_key_exists( 'sendwp/sendwp.php', $plugins ) ) { |
| 273 | |
| 274 | /* |
| 275 | * Use the WordPress Plugins API to get the plugin download link. |
| 276 | */ |
| 277 | $api = plugins_api( 'plugin_information', array( |
| 278 | 'slug' => 'sendwp', |
| 279 | ) ); |
| 280 | |
| 281 | if ( is_wp_error( $api ) ) { |
| 282 | wp_send_json_error( array( |
| 283 | 'error' => $api->get_error_message(), |
| 284 | 'debug' => $api |
| 285 | ) ); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Use the AJAX Upgrader skin to quietly install the plugin. |
| 290 | */ |
| 291 | $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 292 | $install = $upgrader->install( $api->download_link ); |
| 293 | if ( is_wp_error( $install ) ) { |
| 294 | wp_send_json_error( array( |
| 295 | 'error' => $install->get_error_message(), |
| 296 | 'debug' => $api |
| 297 | ) ); |
| 298 | } |
| 299 | |
| 300 | $activated = activate_plugin( $upgrader->plugin_info() ); |
| 301 | |
| 302 | } else { |
| 303 | |
| 304 | $activated = activate_plugin( 'sendwp/sendwp.php' ); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Final check to see if SendWP is available. |
| 310 | */ |
| 311 | if( ! function_exists('sendwp_get_server_url') ) { |
| 312 | wp_send_json_error( array( |
| 313 | 'error' => __( 'Something went wrong. SendWP was not installed correctly.', 'give' ) |
| 314 | ) ); |
| 315 | } |
| 316 | |
| 317 | wp_send_json_success( array( |
| 318 | 'partner_id' => 51154, |
| 319 | 'register_url' => sendwp_get_server_url() . '_/signup', |
| 320 | 'client_name' => sendwp_get_client_name(), |
| 321 | 'client_url' => sendwp_get_client_url(), |
| 322 | 'client_secret' => sendwp_get_client_secret(), |
| 323 | 'client_redirect' => admin_url( '/edit.php?post_type=give_forms&page=give-settings&tab=emails§ion=email-settings' ), |
| 324 | ) ); |
| 325 | } |
| 326 | add_action( 'wp_ajax_give_sendwp_remote_install', 'give_sendwp_remote_install_handler' ); |
| 327 | |
| 328 | /** |
| 329 | * Handle deactivation of SendWP via ajax |
| 330 | * |
| 331 | * @since 2.9.15 |
| 332 | */ |
| 333 | function give_sendwp_disconnect () { |
| 334 | |
| 335 | if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 336 | wp_send_json_error( array( |
| 337 | 'error' => __( 'You do not have permission to do this.', 'give' ) |
| 338 | ) ); |
| 339 | } |
| 340 | |
| 341 | sendwp_disconnect_client(); |
| 342 | |
| 343 | deactivate_plugins( 'sendwp/sendwp.php' ); |
| 344 | |
| 345 | wp_send_json_success(); |
| 346 | } |
| 347 | add_action( 'wp_ajax_give_sendwp_disconnect', 'give_sendwp_disconnect' ); |
| 348 |