PluginProbe
Gianism / 4.3.4
Gianism v4.3.4
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 4.3.4, at functions.php

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