| 1 |
<?php |
| 2 |
|
| 3 |
class Ays_Survey_Maker_Most_Popular_Shortcodes_Public{ |
| 4 |
|
| 5 |
protected $plugin_name; |
| 6 |
private $version; |
| 7 |
private $html_class_prefix = 'ays-survey-most-popular-shortcodes-'; |
| 8 |
private $html_name_prefix = 'ays-survey-'; |
| 9 |
private $name_prefix = 'ays_survey_'; |
| 10 |
private $unique_id; |
| 11 |
private $unique_id_in_class; |
| 12 |
|
| 13 |
public function __construct($plugin_name, $version){ |
| 14 |
|
| 15 |
$this->plugin_name = $plugin_name; |
| 16 |
$this->version = $version; |
| 17 |
|
| 18 |
add_shortcode('ays_survey_most_popular', array($this, 'ays_generate_most_popular_method')); |
| 19 |
} |
| 20 |
|
| 21 |
/* |
| 22 |
========================================== |
| 23 |
Most Popular Survey | Start |
| 24 |
========================================== |
| 25 |
*/ |
| 26 |
|
| 27 |
public function ays_generate_most_popular_method( $attr ){ |
| 28 |
|
| 29 |
$unique_id = uniqid(); |
| 30 |
$this->unique_id = $unique_id; |
| 31 |
$this->unique_id_in_class = "-" . $unique_id; |
| 32 |
|
| 33 |
$survey_most_popular_html = $this->ays_survey_most_popular_html( $attr ); |
| 34 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $survey_most_popular_html); |
| 35 |
} |
| 36 |
|
| 37 |
public function ays_survey_most_popular_html( $attr ){ |
| 38 |
|
| 39 |
$survey_count = (isset( $attr['count'] ) && $attr['count'] != "") ? absint( sanitize_text_field( $attr['count'] ) ) : ""; |
| 40 |
|
| 41 |
$survey_ids = $this->ays_survey_get_most_popular_survey_id( $survey_count ); |
| 42 |
|
| 43 |
$content_html = array(); |
| 44 |
|
| 45 |
if( is_null( $survey_ids ) || empty( $survey_ids ) ){ |
| 46 |
$content_html = "<p style='text-align: center;font-style:italic;'>" . __( "There are no results yet.", "survey-maker" ) . "</p>"; |
| 47 |
return $content_html; |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! empty( $survey_ids ) ) { |
| 51 |
$content_html[] = "<div class='ays-survey-most-popular-container'>"; |
| 52 |
|
| 53 |
foreach ($survey_ids as $key => $survey_id) { |
| 54 |
|
| 55 |
$shortcode = "[ays_survey id='".$survey_id."']"; |
| 56 |
|
| 57 |
$content_html[] = do_shortcode( $shortcode ); |
| 58 |
} |
| 59 |
|
| 60 |
$content_html[] = "</div>"; |
| 61 |
} |
| 62 |
|
| 63 |
$content_html = implode( '' , $content_html); |
| 64 |
|
| 65 |
return $content_html; |
| 66 |
} |
| 67 |
|
| 68 |
public function ays_survey_get_most_popular_survey_id( $survey_count ) { |
| 69 |
global $wpdb; |
| 70 |
|
| 71 |
$submissions_table = esc_sql( $wpdb->prefix . "ayssurvey_submissions" ); |
| 72 |
$surveys_table = esc_sql( $wpdb->prefix . "ayssurvey_surveys" ); |
| 73 |
|
| 74 |
|
| 75 |
$most_popular_survey_count = (isset( $survey_count ) && $survey_count != "") ? absint( sanitize_text_field( $survey_count ) ) : ""; |
| 76 |
$limit = ""; |
| 77 |
|
| 78 |
if($most_popular_survey_count != "" && $most_popular_survey_count != 0){ |
| 79 |
$limit = "LIMIT ".$most_popular_survey_count; |
| 80 |
} |
| 81 |
|
| 82 |
$sql = "SELECT COUNT(*) AS `res_count`, `survey_id` |
| 83 |
FROM `{$submissions_table}` |
| 84 |
GROUP BY `survey_id` |
| 85 |
ORDER BY `res_count` |
| 86 |
DESC ".$limit; |
| 87 |
|
| 88 |
$results = $wpdb->get_results($sql, 'ARRAY_A'); |
| 89 |
$survey_ids = array(); |
| 90 |
if ( ! is_null( $results ) && ! empty( $results ) ) { |
| 91 |
foreach ($results as $key => $value) { |
| 92 |
$id = ( isset( $value['survey_id'] ) && $value['survey_id'] != "" && absint( $value['survey_id'] ) > 0 ) ? absint( sanitize_text_field( $value['survey_id'] ) ) : null; |
| 93 |
$survey_ids[] = $id; |
| 94 |
} |
| 95 |
} |
| 96 |
return $survey_ids; |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
========================================== |
| 101 |
Most Popular Survey | End |
| 102 |
========================================== |
| 103 |
*/ |
| 104 |
|
| 105 |
} |
| 106 |
|