| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global functions for Gianism. |
| 4 |
* |
| 5 |
* @package Gianism |
| 6 |
* @since 1.0 |
| 7 |
* @author Takahashi Fumiki |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Set Cookie wrapper |
| 13 |
* |
| 14 |
* @since 4.2.1 |
| 15 |
* @param string $cookie_name |
| 16 |
* @param string $value |
| 17 |
* @param string $expire |
| 18 |
* @param string $domain |
| 19 |
* @param bool $http_only |
| 20 |
* @param string $same_site_policy |
| 21 |
* @param bool|null $is_ssl |
| 22 |
* @return bool |
| 23 |
*/ |
| 24 |
function gianism_set_cookie( $cookie_name, $value, $expire, $domain = '', $http_only = true, $same_site_policy = 'Lax', $is_ssl = null ) { |
| 25 |
if ( preg_match( '#^https?://([^/:]+)#u', home_url(), $matches ) ) { |
| 26 |
// TODO: Consider domain extraction method. |
| 27 |
$domain = $matches[1]; |
| 28 |
} |
| 29 |
if ( is_null( $is_ssl ) ) { |
| 30 |
$is_ssl = is_ssl(); |
| 31 |
} |
| 32 |
if ( version_compare( phpversion(), '7.3.0', '>=' ) ) { |
| 33 |
return setrawcookie( |
| 34 |
$cookie_name, |
| 35 |
$value, |
| 36 |
[ |
| 37 |
'secure' => $is_ssl, |
| 38 |
'httponly' => $http_only, |
| 39 |
'expires' => $expire, |
| 40 |
'domain' => $domain, |
| 41 |
'path' => '/', |
| 42 |
'samesite' => $same_site_policy, |
| 43 |
] |
| 44 |
); |
| 45 |
} else { |
| 46 |
return setrawcookie( $cookie_name, $value, $expire, '/; SameSite=' . $same_site_policy, $domain, $is_ssl, $http_only ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns if user is connected with particular web service. |
| 52 |
* |
| 53 |
* @package Gianism |
| 54 |
* @since 3.0.0 |
| 55 |
* |
| 56 |
* @param string $service One of facebook, mixi, yahoo, twitter or google. |
| 57 |
* @param int $user_id If not specified, current user id will be used. |
| 58 |
* |
| 59 |
* @return boolean |
| 60 |
*/ |
| 61 |
function gianism_is_user_connected_with( $service, $user_id = 0 ) { |
| 62 |
/** @var \Gianism\Bootstrap $gianism */ |
| 63 |
$gianism = \Gianism\Bootstrap::get_instance(); |
| 64 |
if ( ! $user_id ) { |
| 65 |
$user_id = get_current_user_id(); |
| 66 |
} |
| 67 |
|
| 68 |
return ( $instance = $gianism->service->get( $service ) ) && $instance->is_connected( $user_id ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Get user object by credential |
| 74 |
* |
| 75 |
* Only facebook is supported. |
| 76 |
* |
| 77 |
* @todo Make other services to work. |
| 78 |
* @package Gianism |
| 79 |
* @since 3.0.0 |
| 80 |
* @global \wpdb $wpdb |
| 81 |
* @param string $service |
| 82 |
* @param mixed $credential |
| 83 |
* |
| 84 |
* @return \WP_User |
| 85 |
*/ |
| 86 |
function gianism_get_user_by_service( $service, $credential ) { |
| 87 |
global $wpdb; |
| 88 |
$gianism = \Gianism\Bootstrap::get_instance(); |
| 89 |
$instance = $gianism->service->get( $service ); |
| 90 |
if ( ! $instance ) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
switch ( $service ) { |
| 94 |
case 'facebook': |
| 95 |
$user_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", $instance->umeta_id, $credential ) ); |
| 96 |
if ( $user_id ) { |
| 97 |
return new WP_User( $user_id ); |
| 98 |
} else { |
| 99 |
return null; |
| 100 |
} |
| 101 |
break; |
| 102 |
default: |
| 103 |
return null; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Show Login buttons |
| 110 |
* |
| 111 |
* Show login buttons where you want. |
| 112 |
* |
| 113 |
* @package Gianism |
| 114 |
* @since 1.0 |
| 115 |
* |
| 116 |
* @param string $before |
| 117 |
* @param string $after |
| 118 |
* @param string $redirect_to |
| 119 |
*/ |
| 120 |
function gianism_login( $before = '', $after = '', $redirect_to = '' ) { |
| 121 |
/** @var \Gianism\Controller\Login $login */ |
| 122 |
$login = \Gianism\Controller\Login::get_instance(); |
| 123 |
$login->login_form( $before, $after, false, $redirect_to ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* If user is logged in, display SNS connect buttons. |
| 128 |
* |
| 129 |
* @since 4.3.4 |
| 130 |
* @param |
| 131 |
*/ |
| 132 |
function gianism_connection( $user = null ) { |
| 133 |
if ( is_null( $user ) ) { |
| 134 |
$user = wp_get_current_user(); |
| 135 |
} |
| 136 |
if ( $user ) { |
| 137 |
$profile = \Gianism\Controller\Profile::get_instance(); |
| 138 |
$handler = apply_filters( 'gianism_connect_buttons_handler', $user ); |
| 139 |
if ( is_callable( $handler ) ) { |
| 140 |
$handler( $user ); |
| 141 |
} else { |
| 142 |
$profile->admin_connect_buttons( $user ); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Add UTM campaign link to WordPress admin |
| 149 |
* |
| 150 |
* @package Gianism |
| 151 |
* @since 3.0.0 |
| 152 |
* @internal |
| 153 |
* @param string $url |
| 154 |
* @param array $args |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
function gianism_utm_link( $url, $args = [] ) { |
| 159 |
$args = wp_parse_args( |
| 160 |
$args, |
| 161 |
[ |
| 162 |
'utm_source' => 'wp-admin', |
| 163 |
'utm_medium' => 'link', |
| 164 |
'utm_campaign' => 'Plugin User', |
| 165 |
] |
| 166 |
); |
| 167 |
return add_query_arg( $args, $url ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Detect if WooCommerce is activated |
| 172 |
* |
| 173 |
* @package Gianism |
| 174 |
* @since 3.0.5 |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
function gianism_woocommerce_detected() { |
| 178 |
return class_exists( 'WooCommerce' ); |
| 179 |
} |
| 180 |
|