forked from shramee/freemius-testimonials
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs-testimonial.php
More file actions
217 lines (179 loc) · 5.37 KB
/
fs-testimonial.php
File metadata and controls
217 lines (179 loc) · 5.37 KB
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
216
217
<?php
/*
Plugin Name: Testimonials for Freemius
Description: Shows plugins/theme testimonials from Freemius
Version: 1.1.0
Plugin URI: https://pootlepress.com/freemius-testimonials
Author: Pootlepress
Author URI: https://pootlepress.com/
Domain: fs-testimonial
@developer shramee.srivastav@gmail.com
*/
class FS_Testimonials {
/** @var FS_Testimonials Instance */
private static $_instance;
/**
* Get instance
* @return FS_Testimonials Instance
*/
public static function instance() {
if ( ! self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Returns plugin testimonials
* @param int $plugin Plugin id
* @return array|mixed|null|object|object[]|string
*/
static function get_testimonials( $plugin ) {
if ( ! class_exists( 'Freemius_API' ) ) {
include 'inc/freemius/Freemius.php';
}
$settings = get_option( 'fstm_credentials', array() );
if ( ! $settings ) {
return (object) array(
'error' => array( 'message' => 'API credentials not set.', )
);
}
// Init SDK.
$api = new Freemius_Api(
'developer', //scope
$settings['dev_id'],
$settings['dev_public'],
$settings['dev_secret']
);
// Get all products.
$result = $api->Api( "/plugins/$plugin/reviews.json?is_featured=true" );
return $result;
}
/**
* FS_Testimonials constructor.
*/
public function __construct() {
add_shortcode( 'freemius-testimonials', array( $this, 'testimonials' ) );
add_action( 'admin_init', array( $this, 'admin' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) );
}
public function admin() {
register_setting( 'general', 'fstm_credentials' );
add_settings_section(
'fstm_general_section',
'',
array( $this, 'admin_section_render' ),
'general'
);
}
public function admin_section_render() {
include "inc/tpl.admin-section.php";
}
public function scripts() {
wp_enqueue_style( 'fmt-style', plugin_dir_url( __FILE__ ) . '/assets/front.css', '', '1.0.0' );
// wp_enqueue_script( 'fmt-script', plugin_dir_url( __FILE__ ) . '/assets/front.js', '', '1.0.0' );
}
/**
* Renders testimonials
* @param array $params
* @return string
*/
function testimonials( $params = array() ) {
$params = $params ? $params : array();
$compress = '';
if ( empty( $params['plugin'] ) ) {
return "<p><b>Need plugin id.</b></p>";
}
if ( isset( $params['compress'] ) || in_array( 'compress', $params ) ) {
$compress = 'compress';
}
$testimonials = get_transient( "fs_testimonials_$params[plugin]" );
if ( empty( $testimonials ) ) {
$testimonials = self::get_testimonials( $params['plugin'] );
if ( $testimonials && empty( $testimonials->error ) ) {
set_transient( "fs_testimonials_$params[plugin]", $testimonials, DAY_IN_SECONDS * 7 );
}
}
ob_start();
if ( ! empty( $testimonials->error ) ) {
echo "<p><b>Could not get the testimonials.</b></p>";
if ( WP_DEBUG ) {
echo "<p><b>{$testimonials->error->message}</b></p>";
}
} else {
if ( $testimonials && $testimonials->reviews ) {
$reviews = $testimonials->reviews;
if ( ! empty( $params['order'] ) ) {
$GLOBALS['freemius_testimonials_order'] = array_flip( preg_split( "/[^0-9]+/", $params['order'] ) );
usort( $reviews, function ( $a, $b ) {
$order = $GLOBALS['freemius_testimonials_order'];
$ordera = isset( $order[ $a->id ] ) ? $order[ $a->id ] : 9999;
$orderb = isset( $order[ $b->id ] ) ? $order[ $b->id ] : 9999;
return $ordera > $orderb;
} );
}
$this->render_testimonials( $reviews, $compress );
}
}
return ob_get_clean();
}
public function render_testimonials( $testimonials, $compress ) {
?>
<div id="fs-testimonials" class="<?php echo $compress ?>">
<div class="fs-testimonials-outer-wrap">
<div class="fs-testimonials-wrap">
<?php
$divs = [];
$i = 0;
foreach ( $testimonials as $r ) {
$divs[ $i ++ % 3 ] .= $this->testimonial_html( $r );
}
echo "<div>$divs[0]</div>";
echo "<div>$divs[1]</div>";
echo "<div>$divs[2]</div>";
?>
</div>
</div>
<?php if ( isset( $_GET['fs-testimonial-ids'] ) && current_user_can( 'edit_posts' ) ) { ?>
<style>
#fs-testimonials div[data-id]:after {
content: attr( data-id );
position: absolute;
top: 3px;
right: 7px;
}
</style>
<?php } ?>
<?php if ( $compress ) { ?>
<div
onclick="jQuery(this).closest('#fs-testimonials').toggleClass('compress-expanded')" class="compress-toggle"
data-more="<?php _e( 'More testimonials', 'fs-testimonial' ) ?>"
data-less="<?php _e( 'Less testimonials', 'fs-testimonial' ) ?>">
</div>
<?php } ?>
</div>
<?php
}
function testimonial_html( $r ) {
$r->picture = $r->picture ? $r->picture : 'https://0.gravatar.com/avatar/65e687353c07dd37523cdb8581cec4a9?s=128&d=mm&f=y&r=g';
ob_start();
?>
<div class="testimonial">
<div class="quote-container" data-id="<?php echo $r->id ?>">
<ul class="rate">
<?php
for ( $i = 1; $i < $r->rate + 1; $i += 20 ) {
echo '<li><i class="fa fa-star"></i></li>';
}
?>
</ul>
<h4><?php echo $r->title ?></h4>
<blockquote><p><?php echo $r->text ?></p></blockquote>
<img class="profile-pic" src="<?php echo $r->picture ?>">
</div>
<strong class="name"><?php echo $r->name ?></strong>
</div>
<?php
return ob_get_clean();
}
}
FS_Testimonials::instance();