| 1 |
<?php |
| 2 |
/** |
| 3 |
* Login shortcode class. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Shortcodes/Login |
| 7 |
* @category Class |
| 8 |
* @author Eric Daams |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly |
| 12 |
|
| 13 |
if ( ! class_exists( 'Charitable_Login_Shortcode' ) ) : |
| 14 |
|
| 15 |
/** |
| 16 |
* Charitable_Login_Shortcode class. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class Charitable_Login_Shortcode { |
| 21 |
|
| 22 |
/** |
| 23 |
* The callback method for the campaigns shortcode. |
| 24 |
* |
| 25 |
* This receives the user-defined attributes and passes the logic off to the class. |
| 26 |
* |
| 27 |
* @param array $atts User-defined shortcode attributes. |
| 28 |
* @return string |
| 29 |
* @access public |
| 30 |
* @static |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public static function display( $atts = array() ) { |
| 34 |
|
| 35 |
$defaults = array( |
| 36 |
'logged_in_message' => __( 'You are already logged in!', 'charitable' ), |
| 37 |
'redirect' => esc_url( charitable_get_login_redirect_url() ), |
| 38 |
'registration_link_text' => __( 'Register', 'charitable' ), |
| 39 |
); |
| 40 |
|
| 41 |
$args = shortcode_atts( $defaults, $atts, 'charitable_login' ); |
| 42 |
|
| 43 |
$args['login_form_args'] = self::get_login_form_args( $args ); |
| 44 |
|
| 45 |
if ( is_user_logged_in() ) { |
| 46 |
|
| 47 |
ob_start(); |
| 48 |
|
| 49 |
charitable_template( 'shortcodes/logged-in.php', $args ); |
| 50 |
|
| 51 |
return ob_get_clean(); |
| 52 |
} |
| 53 |
|
| 54 |
if ( false == $args['registration_link_text'] || 'false' == $args['registration_link_text'] ) { |
| 55 |
|
| 56 |
$args['registration_link'] = false; |
| 57 |
|
| 58 |
} else { |
| 59 |
|
| 60 |
$registration_link = charitable_get_permalink( 'registration_page' ); |
| 61 |
|
| 62 |
if ( charitable_get_permalink( 'login_page' ) === $registration_link ) { |
| 63 |
|
| 64 |
$args['registration_link'] = false; |
| 65 |
|
| 66 |
} else { |
| 67 |
|
| 68 |
if ( isset( $_GET['redirect_to'] ) ) { |
| 69 |
|
| 70 |
$registration_link = add_query_arg( 'redirect_to', $_GET['redirect_to'], $registration_link ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
$args['registration_link'] = $registration_link; |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
ob_start(); |
| 80 |
|
| 81 |
charitable_template( 'shortcodes/login.php', $args ); |
| 82 |
|
| 83 |
return apply_filters( 'charitable_login_shortcode', ob_get_clean() ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Fingerprint the login form with our charitable=true hidden field. |
| 89 |
* |
| 90 |
* @param string $content |
| 91 |
* @return string |
| 92 |
* @access public |
| 93 |
* @static |
| 94 |
* @since 1.4.0 |
| 95 |
*/ |
| 96 |
public static function add_hidden_field_to_login_form( $content, $args ) { |
| 97 |
|
| 98 |
if ( isset( $args['charitable'] ) && $args['charitable'] ) { |
| 99 |
$content .= '<input type="hidden" name="charitable" value="1" />'; |
| 100 |
} |
| 101 |
|
| 102 |
return $content; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Return donations to display with the shortcode. |
| 107 |
* |
| 108 |
* @param array $args |
| 109 |
* @return mixed[] $args |
| 110 |
* @access protected |
| 111 |
* @static |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
protected static function get_login_form_args( $args ) { |
| 115 |
$default = array( |
| 116 |
'redirect' => $args['redirect'], |
| 117 |
'charitable' => true, |
| 118 |
); |
| 119 |
|
| 120 |
if ( isset( $_GET['username'] ) ) { |
| 121 |
$default['value_username'] = $_GET['username']; |
| 122 |
} |
| 123 |
|
| 124 |
return apply_filters( 'charitable_login_form_args', $default, $args ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
endif; |
| 129 |
|