forked from oldmill1/post-recommendations-for-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp_recommendations.php
195 lines (139 loc) · 4.16 KB
/
wp_recommendations.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
<?php
/*
Plugin Name: Post Recommendations for WordPress
Plugin URI: http://oldmill1.github.com/post-recommendations-for-wordpress/
Description: Give your visitors more posts to see.
Version: 1.1
Author: Ankur Taxali
Author URI: http://oldmill1.github.com/
License: GPL2
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
include 'recommendationsApp.php';
wp_enqueue_style(
'wp-recommendations-style',
plugins_url( 'stylesheets/stylesheet.css', __FILE__ )
);
wp_enqueue_script("jquery"); // load jQuery for those who don't have it
wp_enqueue_script(
'recommendationsAppJavascript',
plugins_url( 'javascripts/recommendationsApp.js', __FILE__ ),
NULL,
NULL,
TRUE
);
wp_enqueue_script(
'jqueryui',
plugins_url( 'javascripts/jquery-ui.min.js', __FILE__ ),
NULL,
NULL,
TRUE
);
global $app;
$app = new recommendationsApp();
// this can only be done after an app has been started...
$tmth_data = array( 'p' => $app->get_timthumb_path() );
wp_localize_script( 'recommendationsAppJavascript', 'tmth', $tmth_data );
// template tag
function wp_recommendations( $args ) {
global $post;
global $app;
$defaults = array(
'postID' => $post->ID,
'size' => null,
'numberposts' => 6,
'orderby' => 'rand',
'show' => true
);
$args = wp_parse_args( $args, $defaults );
$app->build_recommendations( $args );
}
// shortcode
function recommend_something_func( $atts ) {
global $post;
extract( shortcode_atts( array(
'post_id' => $post->ID,
'size' => null,
'numberposts' => 6,
'orderby' => 'rand',
'show' => false
), $atts ) );
// if size is set, convert it to an array
if ( ! is_null($size) ) {
$size = explode(",", $size, 2 );
$size[0] = intval($size[0]);
$size[1] = intval($size[1]);
}
$args = array(
'postID' => $post_id,
'size' => $size,
'numberposts' => $numberposts,
'orderby' => $orderby,
'show' => false
);
$app = new recommendationsApp();
return $app->build_recommendations( $args );
}
add_shortcode( 'recommend_posts_for_me', 'recommend_something_func' );
// define ajaxurl on the front end
if ( !is_admin() ) :
add_action('wp_head','wprecommendations_ajaxurl');
endif;
function wprecommendations_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}
add_action('wp_ajax_get_posts', 'prefix_ajax_get_posts' );
add_action('wp_ajax_nopriv_get_posts', 'prefix_ajax_get_posts');
// the main ajax request function
function prefix_ajax_get_posts() {
global $app;
global $post;
extract( $_POST ); // imports $type, $id, $num, $orderby
switch ( $type ) {
case "category":
$categories = $id;
$category_in = explode( ",", $categories );
$args = array(
'category__in' => $category_in,
'numberposts' => $num,
'orderby' => $orderby
);
$myposts = get_posts( $args );
$data = array();
foreach ( $myposts as $post ) {
setup_postdata( $post );
$data[$post->ID] = array( "title" => get_the_title(), "imgsrc" => $app->get_image($post->ID), "link" => get_permalink() );
}
exit(json_encode($data));
break;
case "author":
$args = array(
'numberposts' => $num,
'author' => $id,
'orderby' => 'rand'
);
$myposts = get_posts( $args );
$data = array();
foreach ( $myposts as $post ) {
setup_postdata( $post );
$data[$post->ID] = array( "title" => get_the_title(), "imgsrc" => $app->get_image($post->ID), "link" => get_permalink() );
}
exit(json_encode($data));
break;
}
}