Skip to content

Commit 148e757

Browse files
author
jc_ekostyuk
committed
update code
1 parent 1afad20 commit 148e757

3 files changed

Lines changed: 366 additions & 0 deletions

File tree

framework/Admin/Theme_Settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ abstract class Theme_Settings {
1717
*/
1818
protected static $titan_instance;
1919

20+
public static $panel;
21+
2022
/**
2123
* Theme Settings constructor
2224
* init framework hook
Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\Supports;
4+
5+
use JustCoded\WP\Framework\Admin\Theme_Settings;
6+
use Facebook\Facebook as FacebookSDK;
7+
use MetzWeb\Instagram\Instagram as InstagramSDK;
8+
9+
10+
/**
11+
* Class SocialsFeed
12+
*
13+
* @package JustCoded\ThemeFramework\Supports
14+
*/
15+
class Socials_Feed {
16+
17+
/**
18+
* Posts array
19+
*
20+
* @var $fb_posts
21+
*/
22+
public $posts = array();
23+
24+
/**
25+
* SocialsFeed constructor.
26+
*/
27+
public function __construct() {
28+
add_action( 'init', array( $this, 'register_socials_posttype' ) );
29+
add_action( 'init', array( $this, 'register_socials_taxonomy' ) );
30+
add_action( 'init', array( $this, 'create_settings_panel' ) );
31+
32+
add_action( 'jtf_social_sheduler', array( $this, 'get_social_posts' ) );
33+
if ( ! wp_next_scheduled( 'jtf_social_sheduler' ) ) {
34+
wp_schedule_event( time(), 'hourly', 'jtf_social_sheduler' );
35+
}
36+
37+
38+
//add_action( 'init', array( $this, 'insert_posts' ) );
39+
40+
}
41+
42+
/**
43+
* Register Socials post type
44+
*/
45+
public function register_socials_posttype() {
46+
register_post_type( 'socials_feed', array(
47+
'labels' => array(
48+
'name' => 'Socials Feed',
49+
),
50+
'public' => true,
51+
'hierarchical' => true,
52+
'taxonomies' => array(),
53+
) );
54+
}
55+
56+
/**
57+
* Register Socials taxonomy
58+
*/
59+
public function register_socials_taxonomy() {
60+
register_taxonomy( 'socials_category', array( 'socials_feed' ), array(
61+
'label' => '',
62+
'labels' => array(
63+
'name' => 'Socials Category',
64+
),
65+
'public' => true,
66+
'hierarchical' => false,
67+
) );
68+
69+
}
70+
71+
/**
72+
* Create Titan Framework tab for socials API settings
73+
*/
74+
public function create_settings_panel() {
75+
76+
$panel = Theme_Settings::$panel;
77+
78+
$tab = $panel->createTab( array(
79+
'name' => 'Socials Feed',
80+
) );
81+
82+
if ( class_exists( 'Facebook\Facebook' ) ) {
83+
84+
$tab->createOption( array(
85+
'name' => 'Facebook',
86+
'type' => 'heading',
87+
) );
88+
89+
$tab->createOption( array(
90+
'name' => 'Facebook Application ID',
91+
'id' => 'facebook_app_id',
92+
'type' => 'text',
93+
) );
94+
95+
$tab->createOption( array(
96+
'name' => 'Facebook Application Secret',
97+
'id' => 'facebook_app_secret',
98+
'type' => 'text',
99+
) );
100+
101+
$tab->createOption( array(
102+
'name' => 'Facebook Access Token',
103+
'id' => 'facebook_access_token',
104+
'type' => 'text',
105+
) );
106+
107+
}
108+
109+
if ( class_exists( 'Abraham\TwitterOAuth\TwitterOAuth' ) ) {
110+
111+
$tab->createOption( array(
112+
'name' => 'Twitter',
113+
'type' => 'heading',
114+
) );
115+
116+
$tab->createOption( array(
117+
'name' => 'Twitter Customer Key',
118+
'id' => 'twitter_customer_key',
119+
'type' => 'text',
120+
) );
121+
122+
$tab->createOption( array(
123+
'name' => 'Twitter Customer Secret',
124+
'id' => 'twitter_customer_secret',
125+
'type' => 'text',
126+
) );
127+
128+
$tab->createOption( array(
129+
'name' => 'Twitter Access Token',
130+
'id' => 'twitter_access_token',
131+
'type' => 'text',
132+
) );
133+
134+
$tab->createOption( array(
135+
'name' => 'Twitter Access Token Secret',
136+
'id' => 'twitter_access_token_secret',
137+
'type' => 'text',
138+
) );
139+
140+
$tab->createOption( array(
141+
'name' => 'Twitter User Name',
142+
'id' => 'twitter_user_name',
143+
'type' => 'text',
144+
) );
145+
}
146+
147+
if ( class_exists( 'MetzWeb\Instagram\Instagram' ) ) {
148+
149+
$tab->createOption( array(
150+
'name' => 'Instagram',
151+
'type' => 'heading',
152+
) );
153+
154+
$tab->createOption( array(
155+
'name' => 'Instagram User Name',
156+
'id' => 'instagram_user_name',
157+
'type' => 'text',
158+
) );
159+
160+
$tab->createOption( array(
161+
'name' => 'Instagram Client ID',
162+
'id' => 'instagram_api_key',
163+
'type' => 'text',
164+
) );
165+
166+
$tab->createOption( array(
167+
'name' => 'Instagram Client Secret',
168+
'id' => 'instagram_api_secret',
169+
'type' => 'text',
170+
) );
171+
172+
$tab->createOption( array(
173+
'name' => 'Instagram Access Token',
174+
'id' => 'instagram_access_token',
175+
'type' => 'text',
176+
'desc' => 'Get access token on http://instagram.pixelunion.net (you should be logged in your Instagram account in browser)',
177+
) );
178+
179+
}
180+
181+
$tab->createOption( array(
182+
'type' => 'save',
183+
) );
184+
}
185+
186+
/**
187+
* Get posts from Facebook
188+
*
189+
* @return mixed
190+
*/
191+
public function get_fb_posts() {
192+
193+
$facebook_app_id = Theme_Settings::get( 'facebook_app_id' );
194+
$facebook_app_secret = Theme_Settings::get( 'facebook_app_secret' );
195+
$facebook_access_token = Theme_Settings::get( 'facebook_access_token' );
196+
197+
$fb = new FacebookSDK( array(
198+
'app_id' => $facebook_app_id,
199+
'app_secret' => $facebook_app_secret,
200+
'default_graph_version' => 'v2.12',
201+
) );
202+
203+
$response = $fb->get( '/me/feed?fields=full_picture,message,created_time,message_tags,link', $facebook_access_token );
204+
205+
$response_body = $response->getDecodedBody();
206+
207+
$fb_posts = $response_body['data'];
208+
209+
foreach ( $fb_posts as $fb_post ) {
210+
$timestamp = strtotime( $fb_post['created_time'] );
211+
$this->posts[] = array(
212+
'post_title' => 'Facebook post #' . $fb_post['id'],
213+
'post_content' => ( ! empty( $fb_post['message'] ) ) ? $fb_post['message'] : '&nbsp;',
214+
'post_date' => date( 'Y-m-d H:i:s', $timestamp ),
215+
'post_name' => 'facebook_post_' . $fb_post['id'],
216+
'post_type' => 'socials_feed',
217+
'tax_input' => array( 'socials_category' => array( 'facebook' ) ),
218+
'meta_fields' => array(
219+
'postmeta_image' => ( ! empty( $fb_post['full_picture'] ) ) ? $fb_post['full_picture'] : '',
220+
'postmeta_url' => ( ! empty( $fb_post['link'] ) ) ? $fb_post['link'] : '',
221+
),
222+
);
223+
}
224+
225+
return true;
226+
227+
}
228+
229+
/**
230+
* Get posts from Instagram
231+
*
232+
* @return mixed
233+
*/
234+
public function get_insta_posts() {
235+
$instagram_api_key = Theme_Settings::get( 'instagram_api_key' );
236+
$instagram_api_secret = Theme_Settings::get( 'instagram_api_secret' );
237+
$instagram_access_token = Theme_Settings::get( 'instagram_access_token' );
238+
239+
$instagram = new InstagramSDK( array(
240+
'apiKey' => $instagram_api_key,
241+
'apiSecret' => $instagram_api_secret,
242+
'apiCallback' => get_bloginfo( 'url' ),
243+
) );
244+
245+
246+
$insta_posts = null;
247+
248+
if ( $instagram_access_token != false ) {
249+
$instagram->setAccessToken( $instagram_access_token );
250+
$media = $instagram->getUserMedia( 'self', 50 );
251+
$insta_posts = $media->data;
252+
253+
}
254+
255+
256+
foreach ( $insta_posts as $insta_post ) {
257+
//pa($insta_post);
258+
$timestamp = $insta_post->created_time;
259+
$this->posts[] = array(
260+
'post_title' => 'Instagram post #' . $insta_post->id,
261+
'post_content' => ( ! empty( $insta_post->caption->text ) ) ? $insta_post->caption->text : '&nbsp;',
262+
'post_date' => date( 'Y-m-d H:i:s', $timestamp ),
263+
'post_name' => 'instagram_post_' . $insta_post->id,
264+
'post_type' => 'socials_feed',
265+
'tax_input' => array( 'socials_category' => array( 'instagram' ) ),
266+
'meta_fields' => array(
267+
'postmeta_image' => ( ! empty( $insta_post->images->standard_resolution->url ) ) ? $insta_post->images->standard_resolution->url : '',
268+
'postmeta_url' => ( ! empty( $insta_post->link ) ) ? $insta_post->link : '',
269+
'postmeta_url' => ( ! empty( $insta_post->videos ) ) ? $insta_post->videos->standard_resolution->url : '',
270+
),
271+
);
272+
}
273+
274+
return true;
275+
276+
}
277+
278+
/**
279+
* Get posts from Twitter
280+
*/
281+
public function get_twitter_posts() {
282+
283+
284+
$settings = array(
285+
'oauth_access_token' => Theme_Settings::get( 'twitter_access_token' ),
286+
'oauth_access_token_secret' => Theme_Settings::get( 'twitter_access_token_secret' ),
287+
'consumer_key' => Theme_Settings::get( 'twitter_customer_key' ),
288+
'consumer_secret' => Theme_Settings::get( 'twitter_customer_secret' ),
289+
);
290+
291+
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
292+
$getfield = '?screen_name=' . Theme_Settings::get( 'twitter_user_name' );
293+
$requestMethod = 'GET';
294+
295+
$twitter = new \TwitterAPIExchange( $settings );
296+
297+
$twitter_posts = $twitter->setGetfield( $getfield )
298+
->buildOauth( $url, $requestMethod )
299+
->performRequest();
300+
301+
$twitter_posts = json_decode($twitter_posts);
302+
303+
foreach ( $twitter_posts as $twitter_post ) {
304+
$timestamp = strtotime( $twitter_post->created_at );
305+
$this->posts[] = array(
306+
'post_title' => 'Twitter post #' . $twitter_post->id,
307+
'post_content' => ( ! empty( $twitter_post->text ) ) ? $twitter_post->text : '&nbsp;',
308+
'post_date' => date( 'Y-m-d H:i:s', $timestamp ),
309+
'post_name' => 'twitter_post_' . $twitter_post->id,
310+
'post_type' => 'socials_feed',
311+
'post_status' => 'draft',
312+
'tax_input' => array( 'socials_category' => array( 'twitter' ) ),
313+
'meta_fields' => array(
314+
'postmeta_image' => ( ! empty( $twitter_post->entities->media[0]->media_url ) ) ? $twitter_post->entities->media[0]->media_url : '',
315+
'postmeta_url' => 'https://twitter/' . $twitter_post->user->name . '/status/' . $twitter_post->id,
316+
),
317+
);
318+
}
319+
320+
return true;
321+
}
322+
323+
/**
324+
* Insert WP posts
325+
*
326+
* @return bool
327+
*/
328+
public function insert_posts() {
329+
330+
$this->get_fb_posts();
331+
$this->get_insta_posts();
332+
$this->get_twitter_posts();
333+
334+
335+
foreach ( $this->posts as $post ) {
336+
337+
$meta_fields = array();
338+
if ( $this->is_exists( $post['post_title'] ) ) {
339+
continue;
340+
}
341+
if ( isset( $post['meta_fields'] ) ) {
342+
$meta_fields = $post['meta_fields'];
343+
unset( $post['meta_fields'] );
344+
}
345+
346+
$post_id = wp_insert_post( $post );
347+
348+
foreach ( $meta_fields as $meta_key => $meta_value ) {
349+
update_post_meta( $post_id, $meta_key, $meta_value );
350+
}
351+
}
352+
353+
return true;
354+
355+
}
356+
357+
protected function is_exists( $post_title ) {
358+
return get_page_by_title( wp_strip_all_tags( $post_title ), OBJECT, 'socials_feed' );
359+
}
360+
361+
362+
}

framework/Theme.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use JustCoded\WP\Framework\Supports\Just_Tinymce;
1111
use JustCoded\WP\Framework\Web\Template_Hierarchy;
1212
use JustCoded\WP\Framework\Supports\Just_Load_More;
13+
use JustCoded\WP\Framework\Supports\Socials_Feed;
1314
use JustCoded\WP\Framework\Web\View;
1415

16+
1517
/**
1618
* Main base class for theme.
1719
* Init main path rewrite operations and activate / register hooks

0 commit comments

Comments
 (0)