From 148e7579b0fcdfc8c54b3007fa188dda11a04a2a Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Wed, 7 Feb 2018 13:47:17 +0200 Subject: [PATCH 1/6] update code --- framework/Admin/Theme_Settings.php | 2 + framework/Supports/Socials_Feed.php | 362 ++++++++++++++++++++++++++++ framework/Theme.php | 2 + 3 files changed, 366 insertions(+) create mode 100644 framework/Supports/Socials_Feed.php diff --git a/framework/Admin/Theme_Settings.php b/framework/Admin/Theme_Settings.php index 11c1953..81d9d8a 100644 --- a/framework/Admin/Theme_Settings.php +++ b/framework/Admin/Theme_Settings.php @@ -17,6 +17,8 @@ abstract class Theme_Settings { */ protected static $titan_instance; + public static $panel; + /** * Theme Settings constructor * init framework hook diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php new file mode 100644 index 0000000..8be6751 --- /dev/null +++ b/framework/Supports/Socials_Feed.php @@ -0,0 +1,362 @@ + array( + 'name' => 'Socials Feed', + ), + 'public' => true, + 'hierarchical' => true, + 'taxonomies' => array(), + ) ); + } + + /** + * Register Socials taxonomy + */ + public function register_socials_taxonomy() { + register_taxonomy( 'socials_category', array( 'socials_feed' ), array( + 'label' => '', + 'labels' => array( + 'name' => 'Socials Category', + ), + 'public' => true, + 'hierarchical' => false, + ) ); + + } + + /** + * Create Titan Framework tab for socials API settings + */ + public function create_settings_panel() { + + $panel = Theme_Settings::$panel; + + $tab = $panel->createTab( array( + 'name' => 'Socials Feed', + ) ); + + if ( class_exists( 'Facebook\Facebook' ) ) { + + $tab->createOption( array( + 'name' => 'Facebook', + 'type' => 'heading', + ) ); + + $tab->createOption( array( + 'name' => 'Facebook Application ID', + 'id' => 'facebook_app_id', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Facebook Application Secret', + 'id' => 'facebook_app_secret', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Facebook Access Token', + 'id' => 'facebook_access_token', + 'type' => 'text', + ) ); + + } + + if ( class_exists( 'Abraham\TwitterOAuth\TwitterOAuth' ) ) { + + $tab->createOption( array( + 'name' => 'Twitter', + 'type' => 'heading', + ) ); + + $tab->createOption( array( + 'name' => 'Twitter Customer Key', + 'id' => 'twitter_customer_key', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Twitter Customer Secret', + 'id' => 'twitter_customer_secret', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Twitter Access Token', + 'id' => 'twitter_access_token', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Twitter Access Token Secret', + 'id' => 'twitter_access_token_secret', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Twitter User Name', + 'id' => 'twitter_user_name', + 'type' => 'text', + ) ); + } + + if ( class_exists( 'MetzWeb\Instagram\Instagram' ) ) { + + $tab->createOption( array( + 'name' => 'Instagram', + 'type' => 'heading', + ) ); + + $tab->createOption( array( + 'name' => 'Instagram User Name', + 'id' => 'instagram_user_name', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Instagram Client ID', + 'id' => 'instagram_api_key', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Instagram Client Secret', + 'id' => 'instagram_api_secret', + 'type' => 'text', + ) ); + + $tab->createOption( array( + 'name' => 'Instagram Access Token', + 'id' => 'instagram_access_token', + 'type' => 'text', + 'desc' => 'Get access token on http://instagram.pixelunion.net (you should be logged in your Instagram account in browser)', + ) ); + + } + + $tab->createOption( array( + 'type' => 'save', + ) ); + } + + /** + * Get posts from Facebook + * + * @return mixed + */ + public function get_fb_posts() { + + $facebook_app_id = Theme_Settings::get( 'facebook_app_id' ); + $facebook_app_secret = Theme_Settings::get( 'facebook_app_secret' ); + $facebook_access_token = Theme_Settings::get( 'facebook_access_token' ); + + $fb = new FacebookSDK( array( + 'app_id' => $facebook_app_id, + 'app_secret' => $facebook_app_secret, + 'default_graph_version' => 'v2.12', + ) ); + + $response = $fb->get( '/me/feed?fields=full_picture,message,created_time,message_tags,link', $facebook_access_token ); + + $response_body = $response->getDecodedBody(); + + $fb_posts = $response_body['data']; + + foreach ( $fb_posts as $fb_post ) { + $timestamp = strtotime( $fb_post['created_time'] ); + $this->posts[] = array( + 'post_title' => 'Facebook post #' . $fb_post['id'], + 'post_content' => ( ! empty( $fb_post['message'] ) ) ? $fb_post['message'] : ' ', + 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), + 'post_name' => 'facebook_post_' . $fb_post['id'], + 'post_type' => 'socials_feed', + 'tax_input' => array( 'socials_category' => array( 'facebook' ) ), + 'meta_fields' => array( + 'postmeta_image' => ( ! empty( $fb_post['full_picture'] ) ) ? $fb_post['full_picture'] : '', + 'postmeta_url' => ( ! empty( $fb_post['link'] ) ) ? $fb_post['link'] : '', + ), + ); + } + + return true; + + } + + /** + * Get posts from Instagram + * + * @return mixed + */ + public function get_insta_posts() { + $instagram_api_key = Theme_Settings::get( 'instagram_api_key' ); + $instagram_api_secret = Theme_Settings::get( 'instagram_api_secret' ); + $instagram_access_token = Theme_Settings::get( 'instagram_access_token' ); + + $instagram = new InstagramSDK( array( + 'apiKey' => $instagram_api_key, + 'apiSecret' => $instagram_api_secret, + 'apiCallback' => get_bloginfo( 'url' ), + ) ); + + + $insta_posts = null; + + if ( $instagram_access_token != false ) { + $instagram->setAccessToken( $instagram_access_token ); + $media = $instagram->getUserMedia( 'self', 50 ); + $insta_posts = $media->data; + + } + + + foreach ( $insta_posts as $insta_post ) { + //pa($insta_post); + $timestamp = $insta_post->created_time; + $this->posts[] = array( + 'post_title' => 'Instagram post #' . $insta_post->id, + 'post_content' => ( ! empty( $insta_post->caption->text ) ) ? $insta_post->caption->text : ' ', + 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), + 'post_name' => 'instagram_post_' . $insta_post->id, + 'post_type' => 'socials_feed', + 'tax_input' => array( 'socials_category' => array( 'instagram' ) ), + 'meta_fields' => array( + 'postmeta_image' => ( ! empty( $insta_post->images->standard_resolution->url ) ) ? $insta_post->images->standard_resolution->url : '', + 'postmeta_url' => ( ! empty( $insta_post->link ) ) ? $insta_post->link : '', + 'postmeta_url' => ( ! empty( $insta_post->videos ) ) ? $insta_post->videos->standard_resolution->url : '', + ), + ); + } + + return true; + + } + + /** + * Get posts from Twitter + */ + public function get_twitter_posts() { + + + $settings = array( + 'oauth_access_token' => Theme_Settings::get( 'twitter_access_token' ), + 'oauth_access_token_secret' => Theme_Settings::get( 'twitter_access_token_secret' ), + 'consumer_key' => Theme_Settings::get( 'twitter_customer_key' ), + 'consumer_secret' => Theme_Settings::get( 'twitter_customer_secret' ), + ); + + $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; + $getfield = '?screen_name=' . Theme_Settings::get( 'twitter_user_name' ); + $requestMethod = 'GET'; + + $twitter = new \TwitterAPIExchange( $settings ); + + $twitter_posts = $twitter->setGetfield( $getfield ) + ->buildOauth( $url, $requestMethod ) + ->performRequest(); + + $twitter_posts = json_decode($twitter_posts); + + foreach ( $twitter_posts as $twitter_post ) { + $timestamp = strtotime( $twitter_post->created_at ); + $this->posts[] = array( + 'post_title' => 'Twitter post #' . $twitter_post->id, + 'post_content' => ( ! empty( $twitter_post->text ) ) ? $twitter_post->text : ' ', + 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), + 'post_name' => 'twitter_post_' . $twitter_post->id, + 'post_type' => 'socials_feed', + 'post_status' => 'draft', + 'tax_input' => array( 'socials_category' => array( 'twitter' ) ), + 'meta_fields' => array( + 'postmeta_image' => ( ! empty( $twitter_post->entities->media[0]->media_url ) ) ? $twitter_post->entities->media[0]->media_url : '', + 'postmeta_url' => 'https://twitter/' . $twitter_post->user->name . '/status/' . $twitter_post->id, + ), + ); + } + + return true; + } + + /** + * Insert WP posts + * + * @return bool + */ + public function insert_posts() { + + $this->get_fb_posts(); + $this->get_insta_posts(); + $this->get_twitter_posts(); + + + foreach ( $this->posts as $post ) { + + $meta_fields = array(); + if ( $this->is_exists( $post['post_title'] ) ) { + continue; + } + if ( isset( $post['meta_fields'] ) ) { + $meta_fields = $post['meta_fields']; + unset( $post['meta_fields'] ); + } + + $post_id = wp_insert_post( $post ); + + foreach ( $meta_fields as $meta_key => $meta_value ) { + update_post_meta( $post_id, $meta_key, $meta_value ); + } + } + + return true; + + } + + protected function is_exists( $post_title ) { + return get_page_by_title( wp_strip_all_tags( $post_title ), OBJECT, 'socials_feed' ); + } + + +} \ No newline at end of file diff --git a/framework/Theme.php b/framework/Theme.php index 76da1af..ac0e887 100644 --- a/framework/Theme.php +++ b/framework/Theme.php @@ -10,8 +10,10 @@ use JustCoded\WP\Framework\Supports\Just_Tinymce; use JustCoded\WP\Framework\Web\Template_Hierarchy; use JustCoded\WP\Framework\Supports\Just_Load_More; +use JustCoded\WP\Framework\Supports\Socials_Feed; use JustCoded\WP\Framework\Web\View; + /** * Main base class for theme. * Init main path rewrite operations and activate / register hooks From 7dc42fea9d2471e6f28f46c98d1033410545efbf Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Wed, 7 Feb 2018 15:55:16 +0200 Subject: [PATCH 2/6] update code --- framework/Supports/Socials_Feed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php index 8be6751..6adb613 100644 --- a/framework/Supports/Socials_Feed.php +++ b/framework/Supports/Socials_Feed.php @@ -35,7 +35,7 @@ public function __construct() { } - //add_action( 'init', array( $this, 'insert_posts' ) ); + add_action( 'init', array( $this, 'insert_posts' ) ); } From 0d1fa6e7e83e57299f644d4fe9244566b6eec3ac Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Mon, 2 Jul 2018 10:50:30 +0300 Subject: [PATCH 3/6] update SocialFeed without filemode changes --- framework/Supports/Socials_Feed.php | 72 +++++++++++++++++++---------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php index 6adb613..6a58114 100644 --- a/framework/Supports/Socials_Feed.php +++ b/framework/Supports/Socials_Feed.php @@ -21,6 +21,10 @@ class Socials_Feed { */ public $posts = array(); + + const POST_TYPE = 'socials_feed'; + const TAXONOMY = 'socials_category'; + /** * SocialsFeed constructor. */ @@ -34,7 +38,6 @@ public function __construct() { wp_schedule_event( time(), 'hourly', 'jtf_social_sheduler' ); } - add_action( 'init', array( $this, 'insert_posts' ) ); } @@ -43,7 +46,7 @@ public function __construct() { * Register Socials post type */ public function register_socials_posttype() { - register_post_type( 'socials_feed', array( + register_post_type( self::POST_TYPE, array( 'labels' => array( 'name' => 'Socials Feed', ), @@ -57,7 +60,7 @@ public function register_socials_posttype() { * Register Socials taxonomy */ public function register_socials_taxonomy() { - register_taxonomy( 'socials_category', array( 'socials_feed' ), array( + register_taxonomy( self::TAXONOMY, array( self::POST_TYPE ), array( 'label' => '', 'labels' => array( 'name' => 'Socials Category', @@ -190,9 +193,13 @@ public function create_settings_panel() { */ public function get_fb_posts() { - $facebook_app_id = Theme_Settings::get( 'facebook_app_id' ); - $facebook_app_secret = Theme_Settings::get( 'facebook_app_secret' ); - $facebook_access_token = Theme_Settings::get( 'facebook_access_token' ); + $facebook_app_id = Theme_Settings::get( 'facebook_app_id' ); + $facebook_app_secret = Theme_Settings::get( 'facebook_app_secret' ); + $facebook_access_token = Theme_Settings::get( 'facebook_access_token' ); + + if ( empty( $facebook_app_id ) || empty( $facebook_app_secret ) || empty( $facebook_access_token ) ) { + return true; + } $fb = new FacebookSDK( array( 'app_id' => $facebook_app_id, @@ -200,6 +207,7 @@ public function get_fb_posts() { 'default_graph_version' => 'v2.12', ) ); + $response = $fb->get( '/me/feed?fields=full_picture,message,created_time,message_tags,link', $facebook_access_token ); $response_body = $response->getDecodedBody(); @@ -213,8 +221,8 @@ public function get_fb_posts() { 'post_content' => ( ! empty( $fb_post['message'] ) ) ? $fb_post['message'] : ' ', 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), 'post_name' => 'facebook_post_' . $fb_post['id'], - 'post_type' => 'socials_feed', - 'tax_input' => array( 'socials_category' => array( 'facebook' ) ), + 'post_type' => self::POST_TYPE, + 'tax_input' => array( self::TAXONOMY => array( 'facebook' ) ), 'meta_fields' => array( 'postmeta_image' => ( ! empty( $fb_post['full_picture'] ) ) ? $fb_post['full_picture'] : '', 'postmeta_url' => ( ! empty( $fb_post['link'] ) ) ? $fb_post['link'] : '', @@ -236,6 +244,10 @@ public function get_insta_posts() { $instagram_api_secret = Theme_Settings::get( 'instagram_api_secret' ); $instagram_access_token = Theme_Settings::get( 'instagram_access_token' ); + if ( empty( $instagram_api_key ) || empty( $instagram_api_secret ) || empty( $instagram_access_token ) ) { + return true; + } + $instagram = new InstagramSDK( array( 'apiKey' => $instagram_api_key, 'apiSecret' => $instagram_api_secret, @@ -252,17 +264,15 @@ public function get_insta_posts() { } - foreach ( $insta_posts as $insta_post ) { - //pa($insta_post); $timestamp = $insta_post->created_time; $this->posts[] = array( 'post_title' => 'Instagram post #' . $insta_post->id, 'post_content' => ( ! empty( $insta_post->caption->text ) ) ? $insta_post->caption->text : ' ', 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), 'post_name' => 'instagram_post_' . $insta_post->id, - 'post_type' => 'socials_feed', - 'tax_input' => array( 'socials_category' => array( 'instagram' ) ), + 'post_type' => self::POST_TYPE, + 'tax_input' => array( self::TAXONOMY => array( 'instagram' ) ), 'meta_fields' => array( 'postmeta_image' => ( ! empty( $insta_post->images->standard_resolution->url ) ) ? $insta_post->images->standard_resolution->url : '', 'postmeta_url' => ( ! empty( $insta_post->link ) ) ? $insta_post->link : '', @@ -281,11 +291,20 @@ public function get_insta_posts() { public function get_twitter_posts() { + $oauth_access_token = Theme_Settings::get( 'twitter_access_token' ); + $oauth_access_token_secret = Theme_Settings::get( 'twitter_access_token_secret' ); + $consumer_key = Theme_Settings::get( 'twitter_customer_key' ); + $consumer_secret = Theme_Settings::get( 'twitter_customer_secret' ); + + if ( empty( $oauth_access_token ) || empty( $oauth_access_token_secret ) || empty( $consumer_key ) || empty( $consumer_secret ) ) { + return true; + } + $settings = array( - 'oauth_access_token' => Theme_Settings::get( 'twitter_access_token' ), - 'oauth_access_token_secret' => Theme_Settings::get( 'twitter_access_token_secret' ), - 'consumer_key' => Theme_Settings::get( 'twitter_customer_key' ), - 'consumer_secret' => Theme_Settings::get( 'twitter_customer_secret' ), + 'oauth_access_token' => $oauth_access_token, + 'oauth_access_token_secret' => $oauth_access_token_secret, + 'consumer_key' => $consumer_key, + 'consumer_secret' => $consumer_secret, ); $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; @@ -298,7 +317,7 @@ public function get_twitter_posts() { ->buildOauth( $url, $requestMethod ) ->performRequest(); - $twitter_posts = json_decode($twitter_posts); + $twitter_posts = json_decode( $twitter_posts ); foreach ( $twitter_posts as $twitter_post ) { $timestamp = strtotime( $twitter_post->created_at ); @@ -307,9 +326,9 @@ public function get_twitter_posts() { 'post_content' => ( ! empty( $twitter_post->text ) ) ? $twitter_post->text : ' ', 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), 'post_name' => 'twitter_post_' . $twitter_post->id, - 'post_type' => 'socials_feed', + 'post_type' => self::POST_TYPE, 'post_status' => 'draft', - 'tax_input' => array( 'socials_category' => array( 'twitter' ) ), + 'tax_input' => array( self::TAXONOMY => array( 'twitter' ) ), 'meta_fields' => array( 'postmeta_image' => ( ! empty( $twitter_post->entities->media[0]->media_url ) ) ? $twitter_post->entities->media[0]->media_url : '', 'postmeta_url' => 'https://twitter/' . $twitter_post->user->name . '/status/' . $twitter_post->id, @@ -320,6 +339,14 @@ public function get_twitter_posts() { return true; } + + public function get_social_posts() { + + $this->get_fb_posts(); + $this->get_insta_posts(); + $this->get_twitter_posts(); + } + /** * Insert WP posts * @@ -327,11 +354,6 @@ public function get_twitter_posts() { */ public function insert_posts() { - $this->get_fb_posts(); - $this->get_insta_posts(); - $this->get_twitter_posts(); - - foreach ( $this->posts as $post ) { $meta_fields = array(); @@ -355,7 +377,7 @@ public function insert_posts() { } protected function is_exists( $post_title ) { - return get_page_by_title( wp_strip_all_tags( $post_title ), OBJECT, 'socials_feed' ); + return get_page_by_title( wp_strip_all_tags( $post_title ), OBJECT, self::POST_TYPE ); } From 33dcf43a19dfa630437cb6eb776bea6f2412255e Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Mon, 2 Jul 2018 12:59:34 +0300 Subject: [PATCH 4/6] correct posts visibility, change name to slug in is_exist method --- framework/Supports/Socials_Feed.php | 57 ++++++++++++++++------------- framework/Theme.php | 1 - 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php index 6a58114..aa6ac5f 100644 --- a/framework/Supports/Socials_Feed.php +++ b/framework/Supports/Socials_Feed.php @@ -20,6 +20,7 @@ class Socials_Feed { * @var $fb_posts */ public $posts = array(); + public $show_in_admin = true; const POST_TYPE = 'socials_feed'; @@ -38,8 +39,10 @@ public function __construct() { wp_schedule_event( time(), 'hourly', 'jtf_social_sheduler' ); } + add_action( 'init', array( $this, 'get_social_posts' ) ); add_action( 'init', array( $this, 'insert_posts' ) ); + } /** @@ -50,8 +53,8 @@ public function register_socials_posttype() { 'labels' => array( 'name' => 'Socials Feed', ), - 'public' => true, - 'hierarchical' => true, + 'public' => $this->show_in_admin, + 'hierarchical' => false, 'taxonomies' => array(), ) ); } @@ -65,7 +68,7 @@ public function register_socials_taxonomy() { 'labels' => array( 'name' => 'Socials Category', ), - 'public' => true, + 'public' => false, 'hierarchical' => false, ) ); @@ -222,6 +225,7 @@ public function get_fb_posts() { 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), 'post_name' => 'facebook_post_' . $fb_post['id'], 'post_type' => self::POST_TYPE, + 'post_status' => 'publish', 'tax_input' => array( self::TAXONOMY => array( 'facebook' ) ), 'meta_fields' => array( 'postmeta_image' => ( ! empty( $fb_post['full_picture'] ) ) ? $fb_post['full_picture'] : '', @@ -263,22 +267,24 @@ public function get_insta_posts() { $insta_posts = $media->data; } - - foreach ( $insta_posts as $insta_post ) { - $timestamp = $insta_post->created_time; - $this->posts[] = array( - 'post_title' => 'Instagram post #' . $insta_post->id, - 'post_content' => ( ! empty( $insta_post->caption->text ) ) ? $insta_post->caption->text : ' ', - 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), - 'post_name' => 'instagram_post_' . $insta_post->id, - 'post_type' => self::POST_TYPE, - 'tax_input' => array( self::TAXONOMY => array( 'instagram' ) ), - 'meta_fields' => array( - 'postmeta_image' => ( ! empty( $insta_post->images->standard_resolution->url ) ) ? $insta_post->images->standard_resolution->url : '', - 'postmeta_url' => ( ! empty( $insta_post->link ) ) ? $insta_post->link : '', - 'postmeta_url' => ( ! empty( $insta_post->videos ) ) ? $insta_post->videos->standard_resolution->url : '', - ), - ); + if ( ! empty( $insta_posts ) ) { + foreach ( $insta_posts as $insta_post ) { + $timestamp = $insta_post->created_time; + $this->posts[] = array( + 'post_title' => 'Instagram post #' . $insta_post->id, + 'post_content' => ( ! empty( $insta_post->caption->text ) ) ? $insta_post->caption->text : ' ', + 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), + 'post_name' => 'instagram_post_' . $insta_post->id, + 'post_type' => self::POST_TYPE, + 'post_status' => 'publish', + 'tax_input' => array( self::TAXONOMY => array( 'instagram' ) ), + 'meta_fields' => array( + 'postmeta_image' => ( ! empty( $insta_post->images->standard_resolution->url ) ) ? $insta_post->images->standard_resolution->url : '', + 'postmeta_url' => ( ! empty( $insta_post->link ) ) ? $insta_post->link : '', + 'postmeta_url' => ( ! empty( $insta_post->videos ) ) ? $insta_post->videos->standard_resolution->url : '', + ), + ); + } } return true; @@ -314,8 +320,8 @@ public function get_twitter_posts() { $twitter = new \TwitterAPIExchange( $settings ); $twitter_posts = $twitter->setGetfield( $getfield ) - ->buildOauth( $url, $requestMethod ) - ->performRequest(); + ->buildOauth( $url, $requestMethod ) + ->performRequest(); $twitter_posts = json_decode( $twitter_posts ); @@ -327,7 +333,7 @@ public function get_twitter_posts() { 'post_date' => date( 'Y-m-d H:i:s', $timestamp ), 'post_name' => 'twitter_post_' . $twitter_post->id, 'post_type' => self::POST_TYPE, - 'post_status' => 'draft', + 'post_status' => 'publish', 'tax_input' => array( self::TAXONOMY => array( 'twitter' ) ), 'meta_fields' => array( 'postmeta_image' => ( ! empty( $twitter_post->entities->media[0]->media_url ) ) ? $twitter_post->entities->media[0]->media_url : '', @@ -357,7 +363,7 @@ public function insert_posts() { foreach ( $this->posts as $post ) { $meta_fields = array(); - if ( $this->is_exists( $post['post_title'] ) ) { + if ( $this->is_exists( $post['post_name'] ) ) { continue; } if ( isset( $post['meta_fields'] ) ) { @@ -376,9 +382,8 @@ public function insert_posts() { } - protected function is_exists( $post_title ) { - return get_page_by_title( wp_strip_all_tags( $post_title ), OBJECT, self::POST_TYPE ); + protected function is_exists( $post_name ) { + return get_page_by_path( wp_strip_all_tags( $post_name ), OBJECT, self::POST_TYPE ); } - } \ No newline at end of file diff --git a/framework/Theme.php b/framework/Theme.php index ac0e887..709cf39 100644 --- a/framework/Theme.php +++ b/framework/Theme.php @@ -10,7 +10,6 @@ use JustCoded\WP\Framework\Supports\Just_Tinymce; use JustCoded\WP\Framework\Web\Template_Hierarchy; use JustCoded\WP\Framework\Supports\Just_Load_More; -use JustCoded\WP\Framework\Supports\Socials_Feed; use JustCoded\WP\Framework\Web\View; From 55f900d817e38771f8ab1c53b74b6c17d20d0336 Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Mon, 2 Jul 2018 13:20:16 +0300 Subject: [PATCH 5/6] delete debug code --- framework/Supports/Socials_Feed.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php index aa6ac5f..9fce43f 100644 --- a/framework/Supports/Socials_Feed.php +++ b/framework/Supports/Socials_Feed.php @@ -39,7 +39,6 @@ public function __construct() { wp_schedule_event( time(), 'hourly', 'jtf_social_sheduler' ); } - add_action( 'init', array( $this, 'get_social_posts' ) ); add_action( 'init', array( $this, 'insert_posts' ) ); From 1f73ee1dda68a0fad8d225e99827ed880d55591e Mon Sep 17 00:00:00 2001 From: jc_ekostyuk Date: Mon, 2 Jul 2018 18:21:16 +0300 Subject: [PATCH 6/6] correct social feed widgets --- .../v25/Widgets/Socials_Feed_Widget.php | 115 ++++++++++++++++++ framework/Supports/Socials_Feed.php | 2 +- framework/Web/View.php | 21 +++- .../views/widgets/socials-feed-default.php | 29 +++++ templates/views/widgets/socials-feed-full.php | 26 ++++ 5 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 framework/Page_Builder/v25/Widgets/Socials_Feed_Widget.php create mode 100644 templates/views/widgets/socials-feed-default.php create mode 100644 templates/views/widgets/socials-feed-full.php diff --git a/framework/Page_Builder/v25/Widgets/Socials_Feed_Widget.php b/framework/Page_Builder/v25/Widgets/Socials_Feed_Widget.php new file mode 100644 index 0000000..8accfb0 --- /dev/null +++ b/framework/Page_Builder/v25/Widgets/Socials_Feed_Widget.php @@ -0,0 +1,115 @@ + __( 'Socials Feed' ), + 'has_preview' => false, + ), + array(), + false, + '' + ); + } + + + /** + * Form fields configuration + * + * @return array + */ + function get_widget_form() { + return + array( + 'social_networks' => array( + 'type' => 'section', + 'label' => __( 'Choose socials network for show', 'boilerplate' ), + 'hide' => true, + 'fields' => array( + 'facebook' => array( + 'type' => 'checkbox', + 'label' => __( 'Facebook', 'boilerplate' ), + 'default' => false, + ), + 'twitter' => array( + 'type' => 'checkbox', + 'label' => __( 'Twitter', 'boilerplate' ), + 'default' => false, + ), + 'instagram' => array( + 'type' => 'checkbox', + 'label' => __( 'Instagram', 'boilerplate' ), + 'default' => false, + ), + ), + ), + 'widget_type' => array( + 'type' => 'select', + 'label' => __( 'Choose widget type', 'boilerplate' ), + 'options' => array( + self::TYPE_DEFAULT => __( 'Default', 'boilerplate' ), + self::TYPE_FULL => __( 'Full width', 'boilerplate' ), + ), + 'default' => 'full', + ), + ); + + } + + /** + * Modify form submitted values before save. + * + * @param array $instance submitted form values. + * + * @return array + */ + public function modify_instance( $instance ) { + + return $instance; + } + + /** + * Print widget method. + * + * @param array $args Widget display arguments. + * @param array $instance Widget settings. + */ + public function widget( $args, $instance ) { + $instance = $this->get_template_variables( $instance, $args ); + $template = ( self::TYPE_FULL === $instance['widget_type'] ) ? 'socials-feed-full' : 'socials-feed-default'; + + View::instance()->include( 'widgets/' . $template, array( + 'instance' => $instance, + 'before_widget' => $args['before_widget'], + 'after_widget' => $args['after_widget'], + ) ); + } +} \ No newline at end of file diff --git a/framework/Supports/Socials_Feed.php b/framework/Supports/Socials_Feed.php index 9fce43f..79f7894 100644 --- a/framework/Supports/Socials_Feed.php +++ b/framework/Supports/Socials_Feed.php @@ -67,7 +67,7 @@ public function register_socials_taxonomy() { 'labels' => array( 'name' => 'Socials Category', ), - 'public' => false, + 'public' => true, 'hierarchical' => false, ) ); diff --git a/framework/Web/View.php b/framework/Web/View.php index b8ebd55..d23c094 100644 --- a/framework/Web/View.php +++ b/framework/Web/View.php @@ -1,4 +1,5 @@ extends ) ) { + while ( ob_get_contents() && $template = array_pop( $this->extends ) ) { $content = ob_get_contents(); // clean view file buffer. @@ -110,12 +111,12 @@ public function wrap() { * * To use current template generated html use `$content` variable inside the parent view * - * @param string $layout View name to register. + * @param string $layout View name to register. * * @return bool * @throws \Exception If no parent view template found. */ - public function extends( $layout = 'layouts/main' ) { + public function extends ( $layout = 'layouts/main' ) { if ( false === $layout ) { return false; } @@ -133,6 +134,7 @@ public function extends( $layout = 'layouts/main' ) { // start buffer. ob_start(); + return true; } @@ -165,7 +167,7 @@ public function footer_begin( $name = null ) { * * @return bool */ - public function include( $view, $params = array(), $__required = true ) { + public function include ( $view, $params = array(), $__required = true ) { $template = $this->locate( $view, $__required ); if ( empty( $template ) ) { return false; @@ -176,6 +178,7 @@ public function include( $view, $params = array(), $__required = true ) { } include $template; + return true; } @@ -183,8 +186,8 @@ public function include( $view, $params = array(), $__required = true ) { * Return real filename of view, or false if not exists * if view is required then throw error * - * @param string $view view shortname. - * @param boolean $required throw exception if not found. + * @param string $view view shortname. + * @param boolean $required throw exception if not found. * * @return string|false * @@ -193,6 +196,12 @@ public function include( $view, $params = array(), $__required = true ) { public function locate( $view, $required = false ) { $view_file = "views/$view.php"; $template = locate_template( $view_file ); + if ( empty( $template ) ) { + $view_file = "/templates/views/$view.php"; + $template = dirname(__FILE__, 3) . $view_file; + + } + if ( $required && ( empty( $template ) || ! is_file( $template ) ) ) { throw new \Exception( "Unable to locate views template: $view_file" ); } diff --git a/templates/views/widgets/socials-feed-default.php b/templates/views/widgets/socials-feed-default.php new file mode 100644 index 0000000..7d43157 --- /dev/null +++ b/templates/views/widgets/socials-feed-default.php @@ -0,0 +1,29 @@ + + *
+ * + * var $after_widget print: + *
+ * + */ + +$model = new \Boilerplate\Theme\Models\SocialFeeds(); +?> + + + + get_query( $instance )->have_posts() ) : $model->get_query( $instance )->the_post(); ?> + +
+

+

+ "> +
+ + + + + \ No newline at end of file diff --git a/templates/views/widgets/socials-feed-full.php b/templates/views/widgets/socials-feed-full.php new file mode 100644 index 0000000..2ffce0e --- /dev/null +++ b/templates/views/widgets/socials-feed-full.php @@ -0,0 +1,26 @@ + + *
+ * + * var $after_widget print: + *
+ * + */ + +$model = new \Boilerplate\Theme\Models\SocialFeeds(); +?> + + + + get_query( $instance )->have_posts() ) : $model->get_query( $instance )->the_post(); ?> + +

+

+ "> + + + +