forked from shanaver/social-hashtags
/
social_hashtag.php
216 lines (185 loc) · 6.83 KB
/
social_hashtag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
Plugin Name: Social Hashtag
Description: Grabs images & videos matching any hashtag from social APIs like instagram & youtube. Stores thumbnails & details locally for each one in a custom post type so you have full control over the content on your site. This allows you to categorize, make private/public, etc and include them any way that you like on your pages.
Version: 2.3.0
Author: Bryan Shanaver
Author URI: http://fiftyandfifty.org
*/
?>
<?php
define('SOCIAL_HASHTAG_VERSION', '2.3.0');
define('SOCIAL_HASHTAG_URL', plugin_dir_url( __FILE__ ));
define('SOCIAL_HASHTAG_PATH', plugin_dir_path(__FILE__) );
define('SOCIAL_HASHTAG_BASENAME', plugin_basename(__FILE__));
require_once( SOCIAL_HASHTAG_PATH . '/lib/social_hashtag.class.php');
require_once( SOCIAL_HASHTAG_PATH . '/lib/platforms.class.php');
require_once( SOCIAL_HASHTAG_PATH . '/lib/posttypes.php');
require_once( SOCIAL_HASHTAG_PATH . '/lib/shortcodes.php');
// initialize plugin & listen for requests
add_action('init', 'social_hashtag_cache_init', 10);
function social_hashtag_cache_init() {
global $social_hashtag_cache;
$social_hashtag_cache = new SOCIAL_HASHTAG_CACHE();
}
// admin styles & scripts
function social_hashtag_admin_scripts_styles(){
wp_register_style( 'social_hashtag-style', SOCIAL_HASHTAG_URL . 'lib/social_hashtag.css' );
wp_enqueue_style( 'social_hashtag-style' );
wp_localize_script( 'jquery', 'social_hashtag_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'jquery' );
}
add_action('admin_init', 'social_hashtag_admin_scripts_styles');
// front end styles & scripts
function social_hashtag_front_scripts_styles(){
wp_register_style( 'social_hashtag-style', SOCIAL_HASHTAG_URL . 'lib/social_hashtag.css' );
wp_enqueue_style( 'social_hashtag-style' );
}
add_action('wp_enqueue_scripts', 'social_hashtag_front_scripts_styles');
// menu page
function social_hashtag_add_menu_page(){
function social_hashtag_menu_page(){
$options_page_url = SOCIAL_HASHTAG_PATH . '/social_hashtag-options.php';
if(file_exists($options_page_url)){
include_once($options_page_url);
}
else{
print "missing options page!";
}
};
add_submenu_page( 'options-general.php', 'Social Hashtag Settings', 'Social Hashtag', 'switch_themes', 'social_hashtag', 'social_hashtag_menu_page' );
};
add_action( 'admin_menu', 'social_hashtag_add_menu_page' );
// Add settings link on plugin page
function social_hashtag_plugin_settings_link($links) {
$settings_link = '<a href="options-general.php?page=social_hashtag">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
add_filter("plugin_action_links_" . SOCIAL_HASHTAG_BASENAME, 'social_hashtag_plugin_settings_link' );
/*
Cron functions
*/
// set up custom cron schedules
add_filter( 'cron_schedules', 'social_hashtag_cron_schedules');
function social_hashtag_cron_schedules(){
return array(
'every_fifteen_minutes' => array(
'interval' => 60 * 15,
'display' => 'Four Times Hourly'
),
'every_thirty_minutes' => array(
'interval' => 60 * 30,
'display' => 'Twice Hourly'
),
);
}
// function for grabbing all APIs - run from cron
function social_hashtag_grab_apis() {
social_hashtag_logging('Start - cron', 2);
$social_hashtag_cache = new SOCIAL_HASHTAG_CACHE();
$social_hashtag_cache->get_social_hashtag_pics();
social_hashtag_logging('End - cron task finished', 2);
}
add_action('social_hashtag_cron', 'social_hashtag_grab_apis');
// function for adding the cron
function social_hashtag_activate_cron($cron) {
if( wp_get_schedule('social_hashtag_cron') ){
wp_clear_scheduled_hook('social_hashtag_cron');
}
if($cron == '15'){
wp_schedule_event(time(), 'every_fifteen_minutes', 'social_hashtag_cron');
}
elseif($cron == '30'){
wp_schedule_event(time(), 'every_thirty_minutes', 'social_hashtag_cron');
}
else{
wp_schedule_event(time(), 'hourly', 'social_hashtag_cron');
}
}
// function for removing the cron
function social_hashtag_deactivate_cron() {
if( wp_get_schedule('social_hashtag_cron') ){
wp_clear_scheduled_hook('social_hashtag_cron');
}
}
// utility function used by the cron stuff
function social_hashtag_get_shortest_cron($new_cron_interval, $shortest_cron_interval){
$explode = explode("cron", $new_cron_interval, 2);
if( count($explode) == 2 ){
if( (int)$explode[1] < (int)$shortest_cron_interval || empty($shortest_cron_interval)){
return $explode[1];
}
else{
return $shortest_cron_interval;
}
}
else{
return $shortest_cron_interval;
}
}
// when plugin is activated, check for crons that need to be reset
register_activation_hook(__FILE__,'social_hashtag_activate');
function social_hashtag_activate() {
$social_hashtag_cache = new SOCIAL_HASHTAG_CACHE();
$social_api_options = $social_hashtag_cache->get_social_hashtag_options();
$shortest_cron_interval = null;
foreach( $social_api_options as $api_num => $api_settings){
if( !empty($api_settings['api_selected']) ){
$platform = $social_hashtag_cache->choose_platform($api_settings['api_selected']);
if(is_object($platform)){
$shortest_cron_interval = social_hashtag_get_shortest_cron($api_settings['cron_interval'], $shortest_cron_interval);
}
}
}
if( !empty($shortest_cron_interval) ){
social_hashtag_activate_cron($shortest_cron_interval);
}
else{
social_hashtag_deactivate_cron();
}
}
// remove crons on deactivation of plugin
register_deactivation_hook(__FILE__,'social_hashtag_deactivate');
function social_hashtag_deactivate(){
social_hashtag_deactivate_cron();
}
/**
* Fixes the attachment url (so it doesn't look in the local uploads directory)
*
* @package WordPress
*/
add_filter('wp_get_attachment_url', 'social_hashtag_get_attachment_url', 9, 2);
function social_hashtag_get_attachment_url($url, $postID)
{
$social_hashtag_url = get_post_meta($postID, 'social_hashtag_thumb_url', true);
if( !empty($social_hashtag_url) ){
return $social_hashtag_url;
}
else{
return $url;
}
}
/*
Ajax Methods
*/
function social_hashtag_run_request(){
social_hashtag_logging('Start - run manually', 2);
$social_hashtag_cache = new SOCIAL_HASHTAG_CACHE();
if( !empty($_REQUEST['debug']) ){ $social_hashtag_cache->debug = true; }
$results = $social_hashtag_cache->run_social_hashtag_query( $_REQUEST['num'] );
print json_encode(array('success' => true, 'message' => $results ));
social_hashtag_logging('End - run manually: ' . $results, 2);
die();
}
add_action( 'wp_ajax_social_hashtag_run_manually', 'social_hashtag_run_request' );
function social_hashtag_logging($message, $severity=0){
if( !defined('WLS_VERSION') ){
return;
}
$category = 'Social Hashtag Plugin';
if( !wls_is_registered( $category ) ){
wls_register( $category, 'Logging for the Social Hashtag Plugin' );
}
wls_simple_log( $category, $message, $severity);
}