-
Notifications
You must be signed in to change notification settings - Fork 3
/
codeable-reviews-and-expert-profile.php
667 lines (468 loc) · 19.5 KB
/
codeable-reviews-and-expert-profile.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<?php
/*
Plugin Name: Codeable Reviews and Expert Profile
Plugin URI: https://dandulaney.com
GitHub Plugin URI: https://github.com/duplaja/codeable-reviews-and-expert-profile
Description: Gathers Codeable Reviews and Profile Information for a Codeable Expert
Version: 2.4.0
Author: Dan Dulaney
Author URI: https://dandulaney.com
License: GPLv2
License URI:
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
//Automatic Updates from GitHub
require 'plugin-update-checker/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://github.com/duplaja/codeable-reviews-and-expert-profile/',
__FILE__,
'codeable-reviews-and-expert-profile'
);
$myUpdateChecker->setBranch('master');
/*******************************************************************
* Checks to see if Gutenberg is set up on the site before attempting to load blocks
*********************************************************************/
function codeable_reviews_setup_blocks() {
if(function_exists('register_block_type')) {
require_once( plugin_dir_path( __FILE__ ) . 'blocks/codeable-review-list.php');
require_once( plugin_dir_path( __FILE__ ) . 'blocks/codeable-expert-image.php');
require_once( plugin_dir_path( __FILE__ ) . 'blocks/codeable-hire-button.php');
}
}
add_action( 'plugins_loaded', 'codeable_reviews_setup_blocks' );
/**************************************
* Function to enqueue plugin stylesheet
***************************************/
function codeable_css_enqueue() {
wp_enqueue_style( 'codeable-reviews-and-experts-css', plugins_url( 'css/codeable-expert-styles.css', __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'codeable_css_enqueue' );
/****************************************
* Function to pull / store / retrieve expert data
*****************************************/
function codeable_handle_expert_transient($codeable_id) {
// Do we have this information in our transients already?
$transient = get_transient( 'codeable_'.$codeable_id.'_expert' );
// Yep! Just return it and we're done.
if( ! empty( $transient ) ) {
// The function will return here every time after the first time it is run, until the transient expires.
return $transient;
// Nope! We gotta make a call.
} else {
$response = wp_remote_get('https://api.codeable.io/users/'.$codeable_id);
$codeable_expert_data = json_decode(wp_remote_retrieve_body($response));
// Save the API response so we don't have to call again until tomorrow.
set_transient( 'codeable_'.$codeable_id.'_expert', $codeable_expert_data, DAY_IN_SECONDS );
return $codeable_expert_data;
}
}
/****************************************
* Function to pull / store / retrieve expert reviews
*****************************************/
function codeable_handle_review_transient($codeable_id,$number_of_reviews) {
// Do we have this information in our transients already?
$transient = get_transient( 'codeable_'.$codeable_id.'_review_'.$number_of_reviews );
// Yep! Just return it and we're done.
if( ! empty( $transient ) ) {
// The function will return here every time after the first time it is run, until the transient expires.
return $transient;
// Nope! We gotta make a call.
} else {
$four_hours = 4 * HOUR_IN_SECONDS;
$number_of_pages = ceil($number_of_reviews / 4);
$response = wp_remote_get('https://api.codeable.io/users/'.$codeable_id.'/reviews/');
//Returns false if error in connecting
if(wp_remote_retrieve_response_code($response)!= 200) {
return false;
}
$codeable_review_data = json_decode(wp_remote_retrieve_body($response));
if ($number_of_reviews > 4) {
for($i=2;$i<=$number_of_pages;$i++) {
$response = wp_remote_get('https://api.codeable.io/users/'.$codeable_id.'/reviews/?page='.$i);
$temp_review_data = json_decode(wp_remote_retrieve_body($response));
//Breaks out and stops if more reviews are requested than actually exist
if (empty($temp_review_data)) {
$broke_early = true;
break;
}
$codeable_review_data=array_merge($codeable_review_data,$temp_review_data);
}
}
//Parse off any "overages" from grabbing in batches of 4 (only if we didn't break early)
$number_on_last_page = $number_of_reviews % 4;
if ($number_on_last_page != 0 && !isset($broke_early)) {
$number_to_remove = 4 - $number_on_last_page;
$codeable_review_data = array_pop_n($codeable_review_data,$number_to_remove);
}
// Save the API response so we don't have to call again for another hour.
set_transient( 'codeable_'.$codeable_id.'_review_'.$number_of_reviews, $codeable_review_data, $four_hours );
return $codeable_review_data;
}
}
//Removes last n elements of array, utility for caching reviews.
function array_pop_n(array $arr, $n) {
return array_splice($arr, 0, -$n);
}
/****************************
* Function to display the expert image
****************************/
function codeable_display_expert_image( $atts ){
$atts = shortcode_atts(
array(
'codeable_id' => '', //Codeable expert ID #
'circle'=> 'yes', //Whether the image should be circular when displayed
'class'=> 'codeable-profile-image', //Optional extra class to add for easier styling
'loading'=> 'none', //Set to yes to add: loading='lazy'
'cache' => 'no', //Set to yes to save and serve locally
'name' => 'Codeable Expert', //Sets the Expert's Name for the Alt text in image
), $atts, 'expert_image' );
if (empty($atts['codeable_id'])) {
return 'You must enter a valid Codeable Expert ID';
}
//Pulls expert data from API / Transient
$codeable_expert_data = codeable_handle_expert_transient($atts['codeable_id']);
$image= $codeable_expert_data->avatar->large_url;
$profile_img_url = $image;
$name = $atts['name'];
if($atts['cache'] == 'yes') {
$base_profile_img_dir = dirname(__FILE__).'/img/profiles/';
$tempExpertImage = pathinfo($image);
//Show the file name
$filename = $tempExpertImage['basename'];
if(file_exists($base_profile_img_dir.$filename)) {
$profile_img_url = plugins_url( "/img/profiles/"."$filename", __FILE__ );
}
else {
if(file_put_contents( $base_profile_img_dir.$filename,file_get_contents($image))) {
$profile_img_url = plugins_url( "/img/profiles/"."$filename", __FILE__ );
}
else {
$profile_img_url = $image;
}
}
}
$return_image = "<img src='".esc_url($profile_image_url)."'";
if ($atts['circle'] == 'yes') {
$return_image .= " style='border-radius: 50%;'";
}
if ($atts['class'] !='codeable-profile-image') {
$class = $atts['class'];
$return_image .= " class='codeable-profile-image " .esc_attr($class)."'";
} else {
$return_image .= " class='codeable-profile-image'";
}
if($atts['loading'] == 'lazy') {
$return_image .= " loading='lazy'";
}
$return_image.=" alt='${name} Codeable Profile Picture'>";
return $return_image;
}
add_shortcode( 'expert_image', 'codeable_display_expert_image' );
/***************************************
* Function to return expert rating ( returns decimal, text)
****************************************/
function codeable_display_expert_rating($atts) {
$atts = shortcode_atts(
array(
'codeable_id' => '', //Codeable expert ID
), $atts, 'expert_rating' );
if (empty($atts['codeable_id'])) {
return 'You must enter a valid Codeable Expert ID';
}
//Pulls expert data from API / transient
$codeable_expert_data = codeable_handle_expert_transient($atts['codeable_id']);
return $codeable_expert_data->average_rating;
}
add_shortcode( 'expert_rating', 'codeable_display_expert_rating' );
/***********************************************************
* Function to return number of completed tasks (returns int / text)
*************************************************************/
function codeable_display_expert_completed($atts) {
$atts = shortcode_atts(
array(
'codeable_id' => '', //Expert ID
), $atts, 'expert_completed' );
if (empty($atts['codeable_id'])) {
return 'You must enter a valid Codeable Expert ID';
}
$codeable_expert_data = codeable_handle_expert_transient($atts['codeable_id']);
return $codeable_expert_data->completed_tasks_count;
}
add_shortcode( 'expert_completed', 'codeable_display_expert_completed' );
/****************************************************
* Function to create a button for a preferred task (Hire Me)
****************************************************/
function codeable_display_expert_hire($atts) {
$atts = shortcode_atts(
array(
'codeable_id' => '', //Expert ID
'message' => 'Hire Me', //Message / text on button
'referoo' => '', //Referoo code
'class'=> '', //Optional class to add to button, for easier styling
'theme' => 'black', //default theme (white, black, none)
), $atts, 'expert_hire' );
if (empty($atts['codeable_id'])) {
return 'You must enter a valid Codeable Expert ID';
}
$message = $atts['message'];
$codeable_direct_hire_link = 'https://app.codeable.io/tasks/new?preferredContractor='.absint($atts['codeable_id']);
if (!empty($atts['referoo'])) {
$codeable_direct_hire_link.='&ref='.$atts['referoo'];
}
$button = "<a href='$codeable_direct_hire_link' class='codeable-hire-button";
if ($atts['theme']=='black') {
$button.=' hire-button-black';
}
elseif ($atts['theme']== 'white') {
$button.=' hire-button-white';
}
if (!empty($atts['class'])) {
$button.=' '.esc_attr($atts['class']);
}
$button.="'>$message</a>";
return $button;
}
add_shortcode( 'expert_hire', 'codeable_display_expert_hire' );
/**************************************************************
* Function to display expert reviews
**************************************************************/
function codeable_display_reviews($atts){
$atts = shortcode_atts(
array(
'codeable_id' => '', //expert ID
'number_to_show'=>'', //Legacy, not used
'number_to_pull'=>20, //how many to store
'show_title'=>'no', //show project title or not
'show_date'=>'no', //show review date or not
'min_score'=> '', //minimum score to allow (disp)
'max_score'=> '', //maximum score to allow (disp)
'sort'=> '', //order to display in (rand only option currently, beside default new to old)
'start_at' => 1, //Which one of the matching stored to start at, useful for chunking.
'show_x_more' => 0, //How many to display out of matches (0 is max possible)
'min_review_length'=> 0,//Minimum review length to disp
'has_picture'=> 'no', //Control showing only those with set profile images
'show_rating'=> 'yes', //Show rating on each review
'filter_clients' => '', //Comma seperated list of client IDs to filter out
'filter_reviews' => '', //Comma seperated list of review IDs to filter out
'only_clients' => '', //Comma seperated list of client IDs to include (filters all others)
'only_reviews' => '', //Comma seperated list of review IDs to include (filters all others)
'schema' => '', //Send yes to include review schema (once per page only)
'schema_desc' => 'Custom WordPress work through Codeable.io', //Product description for schema
'start_time' => '', //Unix timestamp, shows reviews after this time
'end_time' => '', //Unix timestamp, shows reviews before this time
'loading' => 'none',
'cache' => 'no',
), $atts, 'expert_completed' );
if (empty($atts['codeable_id'])) {
return 'You must enter a valid Codeable Expert ID';
}
//Legacy shortcode att, added to prevent breaking change from number_to_show to better named number_to_pull
if (!empty($atts['number_to_show'])) {
$to_pull = $atts['number_to_show'];
} else {
$to_pull = $atts['number_to_pull'];
}
//Retrieves (from api or transient) all stored reviews
$codeable_review_data = codeable_handle_review_transient($atts['codeable_id'],$to_pull);
if(!$codeable_review_data) {
return '';
}
$schema = $atts['schema'];
if ($schema=='yes') {
$codeable_expert_data = codeable_handle_expert_transient($atts['codeable_id']);
$schema_data = '
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "'.$codeable_expert_data->average_rating.'",
"reviewCount": "'.$codeable_expert_data->completed_tasks_count.'"
},
"description": "'.$atts['schema_desc'].'",
"name": "'.$codeable_expert_data->full_name.'",
"image": "'.$codeable_expert_data->avatar->large_url.'",
"review": [
';
}
//Filters out / removes all with default images, if that att is set
if ($atts['has_picture'] == 'yes') {
$codeable_review_data = array_filter($codeable_review_data,function($review){
if( $review->reviewer->avatar->medium_url != 'https://s3.amazonaws.com/app.codeable.io/avatars/default/medium_default.png') {
return true;
} else {
return false;
}
});
}
//Filters out / removes all with review length less than the minimum
if ($atts['min_review_length'] != 0) {
$min_review_length = $atts['min_review_length'];
$codeable_review_data = array_filter($codeable_review_data,function($review) use($min_review_length){
return strlen($review->comment) >= $min_review_length;
});
}
//Filters out / removes all above the max score
if (!empty($atts['max_score'])) {
$max_score = $atts['max_score'];
$codeable_review_data = array_filter($codeable_review_data,function($review) use($max_score){
return $review->score <= $max_score;
});
}
//Filters out / removes all below the min score
if (!empty($atts['min_score'])) {
$min_score = $atts['min_score'];
$codeable_review_data = array_filter($codeable_review_data,function($review) use($min_score){
return $review->score >= $min_score;
});
}
//Shows only reviews published after the unix timestamp for start time
if (!empty($atts['start_time'])) {
$start_time = $atts['start_time'];
$codeable_review_data = array_filter($codeable_review_data,function($review) use($start_time){
return $review->timestamp >= $start_time;
});
}
//Shows only reviews published before the unix timestamp for end time
if (!empty($atts['end_time'])) {
$end_time = $atts['end_time'];
$codeable_review_data = array_filter($codeable_review_data,function($review) use($end_time){
return $review->timestamp <= $end_time;
});
}
//Filters out matching client ID #'s
if (!empty($atts['filter_clients'])) {
$filter_clients = preg_replace('/\s+/', '', $atts['filter_clients']);
$clients_array = explode(',',$filter_clients);
$codeable_review_data = array_filter($codeable_review_data,function($review) use($clients_array){
return !in_array($review->reviewer->id,$clients_array);
});
}
//Filters out matching review ID #'s
if (!empty($atts['filter_reviews'])) {
$filter_reviews = preg_replace('/\s+/', '', $atts['filter_reviews']);
$reviews_array = explode(',',$filter_reviews);
$codeable_review_data = array_filter($codeable_review_data,function($review) use($reviews_array){
return !in_array($review->id,$reviews_array);
});
}
//Filters out all NON matching client ID #'s
if (!empty($atts['only_clients'])) {
$only_clients = preg_replace('/\s+/', '', $atts['only_clients']);
$clients_array = explode(',',$only_clients);
$codeable_review_data = array_filter($codeable_review_data,function($review) use($clients_array){
return in_array($review->reviewer->id,$clients_array);
});
}
//Filters out all NON matching review ID #'s
if (!empty($atts['only_reviews'])) {
$only_reviews = preg_replace('/\s+/', '', $atts['only_reviews']);
$reviews_array = explode(',',$only_reviews);
$codeable_review_data = array_filter($codeable_review_data,function($review) use($reviews_array){
return in_array($review->id,$reviews_array);
});
}
//Shuffles randomly, doesn't work with the offset
if ($atts['sort'] == 'rand') {
shuffle($codeable_review_data);
}
$to_return = '<ul class="codeable_reviews">';
$review_num = 1;
$showed_this_run = 0;
foreach ($codeable_review_data as $review) {
if ($review_num < $atts['start_at']) {
$review_num++;
continue;
}
$task_title = $review->task_title;
$score = $review->score;
$time = $review->timestamp;
$comment = $review->comment;
$name = $review->reviewer->full_name;
$image = $review->reviewer->avatar->medium_url;
$reviewer_id = $review->reviewer->id;
$review_id = $review->id;
if($schema=='yes') {
if ($showed_this_run != 0) {
$schema_data.=',';
}
$schema_data.='
{
"@type": "Review",
"author": "'.$name.'",
"datePublished": "'.date('Y-m-d',$time).'",
"description": "'. htmlspecialchars( $comment ) .'",
"name": "",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "'.$score.'",
"worstRating": "1"
}
}';
}
$score_disp = '';
if ($atts['show_rating'] != 'no') {
for ($i=0;$i<$score;$i++) {
$score_disp .= "<img src='".plugins_url( 'img/rating-star.png', __FILE__ )."' alt='Rating Star' class='review_rating_star'";
/*if($atts['loading'] == 'lazy') {
$score_disp .= " loading='lazy'";
}*/
$score_disp.= ">";
}
}
$profile_img_url = $image;
if($atts['cache'] == 'yes') {
$base_profile_img_dir = dirname(__FILE__).'/img/profiles/';
//Image Url: $image
$tempReviewerImage = pathinfo($image);
//Show the file name
$filename = $tempReviewerImage['basename'];
if(file_exists($base_profile_img_dir.$filename)) {
$profile_img_url = plugins_url( "/img/profiles/"."$filename", __FILE__ );
}
else {
if(file_put_contents( $base_profile_img_dir.$filename,file_get_contents($image))) {
$profile_img_url = plugins_url( "/img/profiles/"."$filename", __FILE__ );
}
else {
$profile_img_url = $image;
}
}
}
$to_return.= "<li class='codeable_review review_$review_id reviewer_$reviewer_id'>
<img src='".esc_url($profile_img_url)."' alt='Reviewer Image for ".esc_html($name)."' class='reviewer_image'";
if($atts['loading'] == 'lazy') {
$to_return .= " loading='lazy'";
}
$to_return.= ">
<div class='review_info'>";
if($atts['show_title'] == 'yes') {
$to_return.="<p class='review_task_title'>".esc_html($task_title)."</p>";
}
if ($atts['show_rating'] != 'no') {
$to_return.="<p class='review_rating'>Project Rating: <span style='display:inline-block'>".$score_disp."</span></p>";
}
$to_return .= "<p class='review_text'>".esc_html($comment)."</p><p class='reviewer_name'>- ".esc_html($name);
if($atts['show_date'] == 'yes') {
$pretty_date = date('F j Y',$time);
$to_return .=", <span class='review_date'>$pretty_date</span>";
}
$to_return.="</p></div></li>";
$showed_this_run++;
if ($showed_this_run >= $atts['show_x_more'] && $atts['show_x_more'] !=0) {
break;
}
}
$to_return.= '</ul>';
if($schema == 'yes') {
$schema_data.='
]
}
</script>';
$to_return.=$schema_data;
}
return $to_return;
}
add_shortcode('expert_reviews','codeable_display_reviews');