# gianism/3.0.6/functions.php

Gianism, version 3.0.6. 340 lines.

- Page: https://pluginprobe.com/plugins/gianism/3.0.6/code/functions.php
- Raw: https://pluginprobe.com/plugins/gianism/3.0.6/raw/functions.php
- Modified: 2017-09-09T15:40:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/gianism/3.0.6/code/functions.php#L10-L20`.

```php
<?php
/**
 * Global functions for Gianism.
 *
 * @package Gianism
 * @since 1.0
 * @author Takahashi Fumiki
 */


/**
 * Returns Facebook ID
 *
 * @since 3.0.0
 * @package Gianism
 *
 * @param int $user_id
 *
 * @return mixed
 */
function gianism_get_facebook_id( $user_id ) {
	/** @var \Gianism\Service\Facebook $facebook */
	$facebook = \Gianism\Service\Facebook::get_instance();

	return get_user_meta( $user_id, $facebook->umeta_id, true );
}


/**
 * Returns url to get Publish stream permission
 *
 * This function itself has no effect without action hook.
 * See example below:
 *
 * <pre>
 * // In some template, display button.
 * get_facebook_publish_permission_link(get_permalink(), 'my_favorite_action', array('post_id' => get_the_ID()));
 *
 * // Then, hook action in functions.php in your theme.
 * add_action('my_favorite_action', 'my_publish_action');
 *
 * function my_publish_action($facebook, $args){
 *      $post = get_post($args['post_id']);
 *      try{
 *          $facebook->api("/me/feed", "POST", array(
 *          "message" => "I read this article!",
 *          "link" => get_permalink($post->ID),
 *          "name" => get_the_title($post->ID),
 *          "description" => strip_tags($post->post_content),
 *          "action" => json_encode(array(
 *              "name" => get_bloginfo('name'),
 *              "link" => home_url('/')))
 *          ));
 *      }catch(FacebookApiException $e){
 *          // Error
 *          wp_die($e->getMessage());
 *       }
 * }
 * </pre>
 *
 * @since 3.0.0
 * @package Gianism
 *
 * @param string $redirect_url URL where user will be redirect after authentication
 * @param string $action Action name which will be fired after authentication
 * @param array $args Array which will be passed to action hook
 *
 * @return string
 */
function gianism_get_facebook_publish_permission_link( $redirect_url = null, $action = '', $args = [] ) {
	/** @var \Gianism\Service\Facebook $facebook */
	$facebook = \Gianism\Service\Facebook::get_instance();

	return $facebook->get_publish_permission_link( $redirect_url, $action, $args );
}

/**
 * Get facebook API client for admin
 *
 * <pre>
 * $fb = gianism_fb_admin();
 * if( !is_wp_error($fb) ){
 *     // Get feed.
 *     $feeds = $fb->api('/me/feed');
 *     // Post feed.
 *     $fb->api('/me/feed', 'POST', array(
 *         'message' => 'Hola!',
 *     ));
 * }
 * </pre>
 *
 * @package Gianism
 * @since 3.0.0
 * @return \Facebook\Facebook|WP_Error
 */
function gianism_fb_admin() {
	/** @var \Gianism\Service\Facebook $facebook */
	$facebook = \Gianism\Service\Facebook::get_instance();

	return $facebook->admin;
}

/**
 * Get currently set page API
 *
 * @package Gianism
 * @since 3.0.6
 * @return \Facebook\Facebook|WP_Error
 */
function gianism_fb_page_api() {
	return \Gianism\Service\Facebook::get_instance()->get_current_page_api();
}

/**
 * Get admin facebook id
 *
 * This will be user id or facebook page id.
 *
 * <pre>
 * // Example
 * $fb = gianism_fb_admin();
 * if( !is_wp_error($fb) ){
 *     $feed = $fb->api(gianism_fb_admin_id().'/feed');
 *     foreach( $feed['data'] as $status ){
 *         // Do stuff.
 *     }
 * }
 * </pre>
 *
 * @package Gianism
 * @since 3.0.0
 * @return int|string
 */
function gianism_fb_admin_id() {
	/** @var \Gianism\Service\Facebook $facebook */
	$facebook = \Gianism\Service\Facebook::get_instance();

	return $facebook->admin_id;
}

/**
 * Returns if user is connected with particular web service.
 *
 * @package Gianism
 * @since 3.0.0
 *
 * @param string $service One of facebook, mixi, yahoo, twitter or google.
 * @param int $user_id If not specified, current user id will be used.
 *
 * @return boolean
 */
function gianism_is_user_connected_with( $service, $user_id = 0 ) {
	/** @var \Gianism\Bootstrap $gianism */
	$gianism = \Gianism\Bootstrap::get_instance();
	if ( ! $user_id ) {
		$user_id = get_current_user_id();
	}

	return ( $instance = $gianism->service->get( $service ) ) && $instance->is_connected( $user_id );
}


/**
 * Get user object by credential
 *
 * Only facebook is supported.
 *
 * @todo Make other services to work.
 * @package Gianism
 * @since 3.0.0
 * @global \wpdb $wpdb
 * @param string $service
 * @param mixed $credential
 *
 * @return \WP_User
 */
function gianism_get_user_by_service( $service, $credential ) {
	global $wpdb;
	$gianism  = \Gianism\Bootstrap::get_instance();
	$instance = $gianism->service->get( $service );
	if ( ! $instance ) {
		return false;
	}
	switch ( $service ) {
		case 'facebook':
			$user_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s", $credential ) );
			if ( $user_id ) {
				return new WP_User( $user_id );
			} else {
				return null;
			}
			break;
		default:
			return null;
			break;
	}
}

/**
 * Get Twitter Screen Name
 *
 * @package Gianism
 * @since 3.0.0
 *
 * @param int $user_id
 *
 * @return string|false
 */
function gianism_get_twitter_screen_name( $user_id ) {
	/** @var \Gianism\Service\Twitter $twitter */
	$twitter = \Gianism\Service\Twitter::get_instance();

	return get_user_meta( $user_id, $twitter->umeta_screen_name, true );
}


/**
 * Update Twitter time line
 *
 * @package Gianism
 * @since 3.0.0
 *
 * @param string $string
 * @return bool
 */
function gianism_update_twitter_status( $string ) {
	/** @var \Gianism\Service\Twitter $twitter */
	$twitter = \Gianism\Service\Twitter::get_instance();
	$response = $twitter->tweet( $string );
	return ! $response->errors;
}

/**
 * Reply to specified user by Owner Account
 *
 * @package Gianism
 * @since 3.0.0
 *
 * @param int $user_id
 * @param string $string
 *
 * @return boolean
 */
function gianism_twitter_reply_to( $user_id, $string ) {
	$screen_name = gianism_get_twitter_screen_name( $user_id );
	if ( $screen_name ) {
		gianism_update_twitter_status( "@{$screen_name} " . $string );
		return true;
	} else {
		return false;
	}
}

/**
 * Get twitter time line in JSON format object
 *
 * Caching is recommended.
 *
 * <pre>
 * $timeline = get_transient('twitter_timeline');
 * if( false === $timeline){
 *     $timeline = twitter_get_timeline('my_screen_name');
 *     set_transient('twitter_timeline', $timeline, 3600);
 * }
 * foreach($timeline as $status){
 *     // Echo status
 * }
 * </pre>
 *
 * @package Gianism
 * @since 3.0.0
 *
 * @param string $screen_name If not specified, admin user's screen name will be used.
 * @param array $additional_data
 *
 * @return object JSON format object.
 */
function gianism_twitter_get_timeline( $screen_name = null, array $additional_data = [] ) {
	/** @var \Gianism\Service\Twitter $twitter */
	$twitter = \Gianism\Service\Twitter::get_instance();
	if ( is_null( $screen_name ) ) {
		$screen_name = $twitter->tw_screen_name;
	}

	return $twitter->call_api( 'statuses/user_timeline', array_merge(
		array( 'screen_name' => $screen_name ),
		$additional_data
	) );
}


/**
 * Show Login buttons
 *
 * Show login buttons where you want.
 *
 * @package Gianism
 * @since 1.0
 *
 * @param string $before
 * @param string $after
 * @param string $redirect_to
 */
function gianism_login( $before = '', $after = '', $redirect_to = '' ) {
	/** @var \Gianism\Controller\Login $login */
	$login = \Gianism\Controller\Login::get_instance();
	$login->login_form( $before, $after, false, $redirect_to );
}

/**
 * Add UTM campaign link to WordPress admin
 *
 * @package Gianism
 * @since 3.0.0
 * @internal
 * @param string $url
 * @param array  $args
 *
 * @return string
 */
function gianism_utm_link( $url, $args = [] ) {
	$args = wp_parse_args( $args, [
		'utm_source'   => 'wp-admin',
	    'utm_medium'   => 'link',
	    'utm_campaign' => 'Plugin User',
	] );
	return add_query_arg( $args, $url );
}

/**
 * Detect if WooCommerce is activated
 *
 * @package Gianism
 * @since 3.0.5
 * @return bool
 */
function gianism_woocommerce_detected() {
	return class_exists( 'WooCommerce' );
}

```
