| @@ -8,50 +8,123 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * Set Cookie wrapper | |
| 12 | + * Returns Facebook ID | |
| 13 | 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 | |
| 14 | + * @since 3.0.0 | |
| 15 | + * | |
| 16 | + * @param int $user_id | |
| 17 | + * | |
| 18 | + * @return mixed | |
| 23 | 19 | */ |
| 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 | - } | |
| 20 | +function gianism_get_facebook_id( $user_id ) { | |
| 21 | + /** @var \Gianism\Service\Facebook $facebook */ | |
| 22 | + $facebook = \Gianism\Service\Facebook::get_instance(); | |
| 23 | + | |
| 24 | + return get_user_meta( $user_id, $facebook->umeta_id, true ); | |
| 48 | 25 | } |
| 49 | 26 | |
| 27 | + | |
| 50 | 28 | /** |
| 29 | + * Returns url to get Publish stream permission | |
| 30 | + * | |
| 31 | + * This function itself has no effect without action hook. | |
| 32 | + * See example below: | |
| 33 | + * | |
| 34 | + * <pre> | |
| 35 | + * // In some template, display button. | |
| 36 | + * get_facebook_publish_permission_link(get_permalink(), 'my_favorite_action', array('post_id' => get_the_ID())); | |
| 37 | + * | |
| 38 | + * // Then, hook action in functions.php in your theme. | |
| 39 | + * add_action('my_favorite_action', 'my_publish_action'); | |
| 40 | + * | |
| 41 | + * function my_publish_action($facebook, $args){ | |
| 42 | + * $post = get_post($args['post_id']); | |
| 43 | + * try{ | |
| 44 | + * $facebook->api("/me/feed", "POST", array( | |
| 45 | + * "message" => "I read this article!", | |
| 46 | + * "link" => get_permalink($post->ID), | |
| 47 | + * "name" => get_the_title($post->ID), | |
| 48 | + * "description" => strip_tags($post->post_content), | |
| 49 | + * "action" => json_encode(array( | |
| 50 | + * "name" => get_bloginfo('name'), | |
| 51 | + * "link" => home_url('/'))) | |
| 52 | + * )); | |
| 53 | + * }catch(FacebookApiException $e){ | |
| 54 | + * // Error | |
| 55 | + * wp_die($e->getMessage()); | |
| 56 | + * } | |
| 57 | + * } | |
| 58 | + * </pre> | |
| 59 | + * | |
| 60 | + * @since 3.0.0 | |
| 61 | + * | |
| 62 | + * @param string $redirect_url URL where user will be redirect after authentication | |
| 63 | + * @param string $action Action name which will be fired after authentication | |
| 64 | + * @param array $args Array which will be passed to action hook | |
| 65 | + * | |
| 66 | + * @return string | |
| 67 | + */ | |
| 68 | +function gianism_get_facebook_publish_permission_link( $redirect_url = null, $action = '', $args = [] ) { | |
| 69 | + /** @var \Gianism\Service\Facebook $facebook */ | |
| 70 | + $facebook = \Gianism\Service\Facebook::get_instance(); | |
| 71 | + | |
| 72 | + return $facebook->get_publish_permission_link( $redirect_url, $action, $args ); | |
| 73 | +} | |
| 74 | + | |
| 75 | +/** | |
| 76 | + * Get facebook API client for admin | |
| 77 | + * | |
| 78 | + * <pre> | |
| 79 | + * $fb = gianism_fb_admin(); | |
| 80 | + * if( !is_wp_error($fb) ){ | |
| 81 | + * // Get feed. | |
| 82 | + * $feeds = $fb->api('/me/feed'); | |
| 83 | + * // Post feed. | |
| 84 | + * $fb->api('/me/feed', 'POST', array( | |
| 85 | + * 'message' => 'Hola!', | |
| 86 | + * )); | |
| 87 | + * } | |
| 88 | + * </pre> | |
| 89 | + * | |
| 90 | + * @return \Facebook\Facebook|WP_Error | |
| 91 | + */ | |
| 92 | +function gianism_fb_admin() { | |
| 93 | + /** @var \Gianism\Service\Facebook $facebook */ | |
| 94 | + $facebook = \Gianism\Service\Facebook::get_instance(); | |
| 95 | + | |
| 96 | + return $facebook->admin; | |
| 97 | +} | |
| 98 | + | |
| 99 | +/** | |
| 100 | + * Get admin facebook id | |
| 101 | + * | |
| 102 | + * This will be user id or facebook page id. | |
| 103 | + * | |
| 104 | + * <pre> | |
| 105 | + * // Example | |
| 106 | + * $fb = gianism_fb_admin(); | |
| 107 | + * if( !is_wp_error($fb) ){ | |
| 108 | + * $feed = $fb->api(gianism_fb_admin_id().'/feed'); | |
| 109 | + * foreach( $feed['data'] as $status ){ | |
| 110 | + * // Do stuff. | |
| 111 | + * } | |
| 112 | + * } | |
| 113 | + * </pre> | |
| 114 | + * | |
| 115 | + * @return int|string | |
| 116 | + */ | |
| 117 | +function gianism_fb_admin_id() { | |
| 118 | + /** @var \Gianism\Service\Facebook $facebook */ | |
| 119 | + $facebook = \Gianism\Service\Facebook::get_instance(); | |
| 120 | + | |
| 121 | + return $facebook->admin_id; | |
| 122 | +} | |
| 123 | + | |
| 124 | +/** | |
| 51 | 125 | * Returns if user is connected with particular web service. |
| 52 | 126 | * |
| 53 | - * @package Gianism | |
| 54 | 127 | * @since 3.0.0 |
| 55 | 128 | * |
| 56 | 129 | * @param string $service One of facebook, mixi, yahoo, twitter or google. |
| 57 | 130 | * @param int $user_id If not specified, current user id will be used. |
| @@ -64,9 +137,9 @@ | ||
| 64 | 137 | if ( ! $user_id ) { |
| 65 | 138 | $user_id = get_current_user_id(); |
| 66 | 139 | } |
| 67 | 140 | |
| 68 | - return ( $instance = $gianism->service->get( $service ) ) && $instance->is_connected( $user_id ); | |
| 141 | + return ( $instance = $gianism->get_service_instance( $service ) ) && $instance->is_connected( $user_id ); | |
| 69 | 142 | } |
| 70 | 143 | |
| 71 | 144 | |
| 72 | 145 | /** |
| @@ -74,9 +147,8 @@ | ||
| 74 | 147 | * |
| 75 | 148 | * Only facebook is supported. |
| 76 | 149 | * |
| 77 | 150 | * @todo Make other services to work. |
| 78 | - * @package Gianism | |
| 79 | 151 | * @since 3.0.0 |
| 80 | 152 | * @global \wpdb $wpdb |
| 81 | 153 | * @param string $service |
| 82 | 154 | * @param mixed $credential |
| @@ -85,16 +157,15 @@ | ||
| 85 | 157 | */ |
| 86 | 158 | function gianism_get_user_by_service( $service, $credential ) { |
| 87 | 159 | global $wpdb; |
| 88 | 160 | $gianism = \Gianism\Bootstrap::get_instance(); |
| 89 | - $instance = $gianism->service->get( $service ); | |
| 161 | + $instance = $gianism->get_instance( $service ); | |
| 90 | 162 | if ( ! $instance ) { |
| 91 | 163 | return false; |
| 92 | 164 | } |
| 93 | 165 | switch ( $service ) { |
| 94 | 166 | 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 ) ); | |
| 167 | + $user_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s", $credential ) ); | |
| 97 | 168 | if ( $user_id ) { |
| 98 | 169 | return new WP_User( $user_id ); |
| 99 | 170 | } else { |
| 100 | 171 | return null; |
| @@ -106,13 +177,99 @@ | ||
| 106 | 177 | } |
| 107 | 178 | } |
| 108 | 179 | |
| 109 | 180 | /** |
| 181 | + * Get Twitter Screen Name | |
| 182 | + * | |
| 183 | + * @since 3.0.0 | |
| 184 | + * | |
| 185 | + * @param int $user_id | |
| 186 | + * | |
| 187 | + * @return string|false | |
| 188 | + */ | |
| 189 | +function gianism_get_twitter_screen_name( $user_id ) { | |
| 190 | + /** @var \Gianism\Service\Twitter $twitter */ | |
| 191 | + $twitter = \Gianism\Service\Twitter::get_instance(); | |
| 192 | + | |
| 193 | + return get_user_meta( $user_id, $twitter->umeta_screen_name, true ); | |
| 194 | +} | |
| 195 | + | |
| 196 | + | |
| 197 | +/** | |
| 198 | + * Update Twitter time line | |
| 199 | + * | |
| 200 | + * @since 3.0.0 | |
| 201 | + * | |
| 202 | + * @param string $string | |
| 203 | + */ | |
| 204 | +function gianism_update_twitter_status( $string ) { | |
| 205 | + /** @var \Gianism\Service\Twitter $twitter */ | |
| 206 | + $twitter = \Gianism\Service\Twitter::get_instance(); | |
| 207 | + $twitter->tweet( $string ); | |
| 208 | +} | |
| 209 | + | |
| 210 | +/** | |
| 211 | + * Reply to specified user by Owner Account | |
| 212 | + * | |
| 213 | + * @since 3.0.0 | |
| 214 | + * | |
| 215 | + * @param int $user_id | |
| 216 | + * @param string $string | |
| 217 | + * | |
| 218 | + * @return boolean | |
| 219 | + */ | |
| 220 | +function gianism_twitter_reply_to( $user_id, $string ) { | |
| 221 | + $screen_name = gianism_get_twitter_screen_name( $user_id ); | |
| 222 | + if ( $screen_name ) { | |
| 223 | + gianism_update_twitter_status( "@{$screen_name} " . $string ); | |
| 224 | + return true; | |
| 225 | + } else { | |
| 226 | + return false; | |
| 227 | + } | |
| 228 | +} | |
| 229 | + | |
| 230 | +/** | |
| 231 | + * Get twitter time line in JSON format object | |
| 232 | + * | |
| 233 | + * Caching is recommended. | |
| 234 | + * | |
| 235 | + * <pre> | |
| 236 | + * $timeline = get_transient('twitter_timeline'); | |
| 237 | + * if( false === $timeline){ | |
| 238 | + * $timeline = twitter_get_timeline('my_screen_name'); | |
| 239 | + * set_transient('twitter_timeline', $timeline, 3600); | |
| 240 | + * } | |
| 241 | + * foreach($timeline as $status){ | |
| 242 | + * // Echo status | |
| 243 | + * } | |
| 244 | + * </pre> | |
| 245 | + * | |
| 246 | + * @since 3.0.0 | |
| 247 | + * | |
| 248 | + * @param string $screen_name If not specified, admin user's screen name will be used. | |
| 249 | + * @param array $additional_data | |
| 250 | + * | |
| 251 | + * @return object JSON format object. | |
| 252 | + */ | |
| 253 | +function gianism_twitter_get_timeline( $screen_name = null, array $additional_data = [] ) { | |
| 254 | + /** @var \Gianism\Service\Twitter $twitter */ | |
| 255 | + $twitter = \Gianism\Service\Twitter::get_instance(); | |
| 256 | + if ( is_null( $screen_name ) ) { | |
| 257 | + $screen_name = $twitter->tw_screen_name; | |
| 258 | + } | |
| 259 | + | |
| 260 | + return $twitter->call_api( 'statuses/user_timeline', array_merge( | |
| 261 | + array( 'screen_name' => $screen_name ), | |
| 262 | + $additional_data | |
| 263 | + ) ); | |
| 264 | +} | |
| 265 | + | |
| 266 | + | |
| 267 | +/** | |
| 110 | 268 | * Show Login buttons |
| 111 | 269 | * |
| 112 | 270 | * Show login buttons where you want. |
| 113 | 271 | * |
| 114 | - * @package Gianism | |
| 115 | 272 | * @since 1.0 |
| 116 | 273 | * |
| 117 | 274 | * @param string $before |
| 118 | 275 | * @param string $after |
| @@ -124,34 +281,10 @@ | ||
| 124 | 281 | $login->login_form( $before, $after, false, $redirect_to ); |
| 125 | 282 | } |
| 126 | 283 | |
| 127 | 284 | /** |
| 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 | 285 | * Add UTM campaign link to WordPress admin |
| 150 | 286 | * |
| 151 | - * @package Gianism | |
| 152 | - * @since 3.0.0 | |
| 153 | - * @internal | |
| 154 | 287 | * @param string $url |
| 155 | 288 | * @param array $args |
| 156 | 289 | * |
| 157 | 290 | * @return string |
| @@ -156,25 +289,11 @@ | ||
| 156 | 289 | * |
| 157 | 290 | * @return string |
| 158 | 291 | */ |
| 159 | 292 | 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 | - ); | |
| 293 | + $args = wp_parse_args( $args, [ | |
| 294 | + 'utm_source' => 'wp-admin', | |
| 295 | + 'utm_medium' => 'link', | |
| 296 | + 'utm_campaign' => 'Plugin User', | |
| 297 | + ] ); | |
| 168 | 298 | 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 | 299 | } |