PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / functions.php

functions.php in Gianism trunk, at functions.php

181 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /** @var \Gianism\Service\Facebook $instance */
96 $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 ) );
97 if ( $user_id ) {
98 return new WP_User( $user_id );
99 } else {
100 return null;
101 }
102 break;
103 default:
104 return null;
105 break;
106 }
107 }
108
109 /**
110 * Show Login buttons
111 *
112 * Show login buttons where you want.
113 *
114 * @package Gianism
115 * @since 1.0
116 *
117 * @param string $before
118 * @param string $after
119 * @param string $redirect_to
120 */
121 function gianism_login( $before = '', $after = '', $redirect_to = '' ) {
122 /** @var \Gianism\Controller\Login $login */
123 $login = \Gianism\Controller\Login::get_instance();
124 $login->login_form( $before, $after, false, $redirect_to );
125 }
126
127 /**
128 * If user is logged in, display SNS connect buttons.
129 *
130 * @since 4.3.4
131 * @param \WP_User|null $user User object. Default current user.
132 */
133 function gianism_connection( $user = null ) {
134 if ( is_null( $user ) ) {
135 $user = wp_get_current_user();
136 }
137 if ( $user ) {
138 $profile = \Gianism\Controller\Profile::get_instance();
139 $handler = apply_filters( 'gianism_connect_buttons_handler', $user );
140 if ( is_callable( $handler ) ) {
141 $handler( $user );
142 } else {
143 $profile->admin_connect_buttons( $user );
144 }
145 }
146 }
147
148 /**
149 * Add UTM campaign link to WordPress admin
150 *
151 * @package Gianism
152 * @since 3.0.0
153 * @internal
154 * @param string $url
155 * @param array $args
156 *
157 * @return string
158 */
159 function gianism_utm_link( $url, $args = [] ) {
160 $args = wp_parse_args(
161 $args,
162 [
163 'utm_source' => 'wp-admin',
164 'utm_medium' => 'link',
165 'utm_campaign' => 'Plugin User',
166 ]
167 );
168 return add_query_arg( $args, $url );
169 }
170
171 /**
172 * Detect if WooCommerce is activated
173 *
174 * @package Gianism
175 * @since 3.0.5
176 * @return bool
177 */
178 function gianism_woocommerce_detected() {
179 return class_exists( 'WooCommerce' );
180 }
181