| 1 |
<?php |
| 2 |
|
| 3 |
class Ays_Survey_Maker_Extra_Shortcodes_Public { |
| 4 |
protected $plugin_name; |
| 5 |
private $version; |
| 6 |
|
| 7 |
private $html_class_prefix = 'ays-survey-extra-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 |
protected $default_texts; |
| 13 |
|
| 14 |
public function __construct($plugin_name, $version){ |
| 15 |
|
| 16 |
$this->plugin_name = $plugin_name; |
| 17 |
$this->version = $version; |
| 18 |
$shortcodes = array( |
| 19 |
"user_first_name", |
| 20 |
"user_last_name" , |
| 21 |
"user_nick_name" , |
| 22 |
"user_display_name" , |
| 23 |
"user_email" , |
| 24 |
"user_wordpress_roles" , |
| 25 |
"passed_users_count" , |
| 26 |
"creation_date" , |
| 27 |
"sections_count" , |
| 28 |
"questions_count" , |
| 29 |
"categories_count" , |
| 30 |
); |
| 31 |
|
| 32 |
foreach($shortcodes as $shortcode){ |
| 33 |
add_shortcode('ays_survey_'.$shortcode, array($this, 'ays_generate_'.$shortcode.'_method')); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
/* |
| 40 |
========================================== |
| 41 |
Show users firstname | Start |
| 42 |
========================================== |
| 43 |
*/ |
| 44 |
public function ays_generate_user_first_name_method(){ |
| 45 |
|
| 46 |
$unique_id = uniqid(); |
| 47 |
$this->unique_id = $unique_id; |
| 48 |
$this->unique_id_in_class = $unique_id; |
| 49 |
|
| 50 |
$user_first_name_html = ""; |
| 51 |
if(is_user_logged_in()){ |
| 52 |
$user_first_name_html = $this->ays_generate_users_html('first'); |
| 53 |
} |
| 54 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_first_name_html); |
| 55 |
} |
| 56 |
/* |
| 57 |
========================================== |
| 58 |
Show users firstname | End |
| 59 |
========================================== |
| 60 |
*/ |
| 61 |
|
| 62 |
/* |
| 63 |
========================================== |
| 64 |
Show users lastname | Start |
| 65 |
========================================== |
| 66 |
*/ |
| 67 |
public function ays_generate_user_last_name_method(){ |
| 68 |
|
| 69 |
$unique_id = uniqid(); |
| 70 |
$this->unique_id = $unique_id; |
| 71 |
$this->unique_id_in_class = $unique_id; |
| 72 |
|
| 73 |
$user_last_name_html = ""; |
| 74 |
if(is_user_logged_in()){ |
| 75 |
$user_last_name_html = $this->ays_generate_users_html('last'); |
| 76 |
} |
| 77 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_last_name_html); |
| 78 |
} |
| 79 |
/* |
| 80 |
========================================== |
| 81 |
Show users lastname | End |
| 82 |
========================================== |
| 83 |
*/ |
| 84 |
/* |
| 85 |
========================================== |
| 86 |
Show users displayname | Start |
| 87 |
========================================== |
| 88 |
*/ |
| 89 |
public function ays_generate_user_display_name_method(){ |
| 90 |
|
| 91 |
$unique_id = uniqid(); |
| 92 |
$this->unique_id = $unique_id; |
| 93 |
$this->unique_id_in_class = $unique_id; |
| 94 |
|
| 95 |
$user_last_name_html = ""; |
| 96 |
if(is_user_logged_in()){ |
| 97 |
$user_last_name_html = $this->ays_generate_users_html('display'); |
| 98 |
} |
| 99 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_last_name_html); |
| 100 |
} |
| 101 |
/* |
| 102 |
========================================== |
| 103 |
Show users displayname | End |
| 104 |
========================================== |
| 105 |
*/ |
| 106 |
/* |
| 107 |
========================================== |
| 108 |
Show users nick name | Start |
| 109 |
========================================== |
| 110 |
*/ |
| 111 |
public function ays_generate_user_nick_name_method(){ |
| 112 |
|
| 113 |
$unique_id = uniqid(); |
| 114 |
$this->unique_id = $unique_id; |
| 115 |
$this->unique_id_in_class = $unique_id; |
| 116 |
|
| 117 |
$user_last_name_html = ""; |
| 118 |
if(is_user_logged_in()){ |
| 119 |
$user_last_name_html = $this->ays_generate_users_html('nick'); |
| 120 |
} |
| 121 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_last_name_html); |
| 122 |
} |
| 123 |
/* |
| 124 |
========================================== |
| 125 |
Show users nick name | End |
| 126 |
========================================== |
| 127 |
*/ |
| 128 |
/* |
| 129 |
========================================== |
| 130 |
Show users nick email | Start |
| 131 |
========================================== |
| 132 |
*/ |
| 133 |
public function ays_generate_user_email_method(){ |
| 134 |
|
| 135 |
$unique_id = uniqid(); |
| 136 |
$this->unique_id = $unique_id; |
| 137 |
$this->unique_id_in_class = $unique_id; |
| 138 |
|
| 139 |
$user_last_email_html = ""; |
| 140 |
if(is_user_logged_in()){ |
| 141 |
$user_last_email_html = $this->ays_generate_users_html('email'); |
| 142 |
} |
| 143 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_last_email_html); |
| 144 |
} |
| 145 |
/* |
| 146 |
========================================== |
| 147 |
Show users nick email | End |
| 148 |
========================================== |
| 149 |
*/ |
| 150 |
/* |
| 151 |
========================================== |
| 152 |
Show users nick email | Start |
| 153 |
========================================== |
| 154 |
*/ |
| 155 |
public function ays_generate_user_wordpress_roles_method(){ |
| 156 |
|
| 157 |
$unique_id = uniqid(); |
| 158 |
$this->unique_id = $unique_id; |
| 159 |
$this->unique_id_in_class = $unique_id; |
| 160 |
|
| 161 |
$user_last_email_html = ""; |
| 162 |
if(is_user_logged_in()){ |
| 163 |
$user_last_email_html = $this->ays_generate_users_html('wordpress_roles'); |
| 164 |
} |
| 165 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $user_last_email_html); |
| 166 |
} |
| 167 |
/* |
| 168 |
========================================== |
| 169 |
Show users nick email | End |
| 170 |
========================================== |
| 171 |
*/ |
| 172 |
|
| 173 |
/* |
| 174 |
========================================== |
| 175 |
Passed users count | Start |
| 176 |
========================================== |
| 177 |
*/ |
| 178 |
public function ays_generate_passed_users_count_method( $attr ){ |
| 179 |
|
| 180 |
$id = (isset($attr['id']) && $attr['id'] != '') ? absint( sanitize_text_field($attr['id']) ) : null; |
| 181 |
$this->default_texts = Survey_Maker_Data::ays_set_default_texts( $this->plugin_name, array() ); |
| 182 |
|
| 183 |
if (is_null($id) || $id == 0 ) { |
| 184 |
$passed_users_count_html = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>"; |
| 185 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $passed_users_count_html); |
| 186 |
} |
| 187 |
|
| 188 |
$unique_id = uniqid(); |
| 189 |
$this->unique_id = $unique_id; |
| 190 |
$this->unique_id_in_class = $id . "-" . $unique_id; |
| 191 |
|
| 192 |
|
| 193 |
$passed_users_count_html = $this->ays_survey_passed_users_count_html( $id ); |
| 194 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $passed_users_count_html); |
| 195 |
} |
| 196 |
|
| 197 |
public function ays_survey_passed_users_count_html( $id ){ |
| 198 |
|
| 199 |
$results = Survey_Maker_Data::get_survey_results_count_by_id($id); |
| 200 |
|
| 201 |
$content_html = array(); |
| 202 |
|
| 203 |
if($results === null){ |
| 204 |
$content_html = "<p style='text-align: center;font-style:italic;'>" . __( "There are no results yet.", "survey-maker" ) . "</p>"; |
| 205 |
return $content_html; |
| 206 |
} |
| 207 |
|
| 208 |
$passed_users_count = (isset( $results['res_count'] ) && $results['res_count'] != '') ? sanitize_text_field( $results['res_count'] ) : 0; |
| 209 |
|
| 210 |
$content_html[] = "<span class='". $this->html_name_prefix ."passed-users-count-box' id='". $this->html_name_prefix ."passed-users-count-box-". $this->unique_id_in_class ."' data-id='". $this->unique_id ."'>"; |
| 211 |
$content_html[] = $passed_users_count; |
| 212 |
$content_html[] = "</span>"; |
| 213 |
|
| 214 |
$content_html = implode( '' , $content_html); |
| 215 |
|
| 216 |
return $content_html; |
| 217 |
} |
| 218 |
/* |
| 219 |
========================================== |
| 220 |
Passed users count | End |
| 221 |
========================================== |
| 222 |
*/ |
| 223 |
|
| 224 |
/* |
| 225 |
========================================== |
| 226 |
Survey show creation date | Start |
| 227 |
========================================== |
| 228 |
*/ |
| 229 |
public function ays_generate_creation_date_method( $attr ){ |
| 230 |
|
| 231 |
$id = (isset($attr['id']) && $attr['id'] != '') ? absint( sanitize_text_field($attr['id']) ) : null; |
| 232 |
$this->default_texts = Survey_Maker_Data::ays_set_default_texts( $this->plugin_name, array() ); |
| 233 |
|
| 234 |
if (is_null($id) || $id == 0 ) { |
| 235 |
$surve_creation_date_html = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>"; |
| 236 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $surve_creation_date_html); |
| 237 |
} |
| 238 |
|
| 239 |
$unique_id = uniqid(); |
| 240 |
$this->unique_id = $unique_id; |
| 241 |
$this->unique_id_in_class = $id . "-" . $unique_id; |
| 242 |
|
| 243 |
|
| 244 |
$surve_creation_date_html = $this->ays_survey_creation_date_html( $id ); |
| 245 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $surve_creation_date_html); |
| 246 |
} |
| 247 |
|
| 248 |
public function ays_survey_creation_date_html( $id ){ |
| 249 |
|
| 250 |
$results = $this->get_curent_survey_creation_date($id); |
| 251 |
|
| 252 |
$content_html = array(); |
| 253 |
|
| 254 |
if($results === null){ |
| 255 |
$content_html = "<p style='text-align: center;font-style:italic;'>" . __( "There are no results yet.", "survey-maker" ) . "</p>"; |
| 256 |
return $content_html; |
| 257 |
} |
| 258 |
|
| 259 |
$content_html[] = "<span class='". $this->html_name_prefix ."creation-date-box' id='". $this->html_name_prefix ."creation-date-box-". $this->unique_id_in_class ."' data-id='". $this->unique_id ."'>"; |
| 260 |
$content_html[] = $results; |
| 261 |
$content_html[] = "</span>"; |
| 262 |
|
| 263 |
$content_html = implode( '' , $content_html); |
| 264 |
|
| 265 |
return $content_html; |
| 266 |
} |
| 267 |
/* |
| 268 |
========================================== |
| 269 |
Survey show creation date | End |
| 270 |
========================================== |
| 271 |
*/ |
| 272 |
|
| 273 |
/* |
| 274 |
========================================== |
| 275 |
Survey show sections count | Start |
| 276 |
========================================== |
| 277 |
*/ |
| 278 |
public function ays_generate_sections_count_method( $attr ){ |
| 279 |
|
| 280 |
$id = (isset($attr['id']) && $attr['id'] != '') ? absint( sanitize_text_field($attr['id']) ) : null; |
| 281 |
$this->default_texts = Survey_Maker_Data::ays_set_default_texts( $this->plugin_name, array() ); |
| 282 |
|
| 283 |
if (is_null($id) || $id == 0 ) { |
| 284 |
$survey_sections_count_html = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>"; |
| 285 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $survey_sections_count_html); |
| 286 |
} |
| 287 |
|
| 288 |
$unique_id = uniqid(); |
| 289 |
$this->unique_id = $unique_id; |
| 290 |
$this->unique_id_in_class = $id . "-" . $unique_id; |
| 291 |
|
| 292 |
|
| 293 |
$survey_sections_count_html = $this->ays_survey_questions_sections_count_html( $id , 'sections' ); |
| 294 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $survey_sections_count_html); |
| 295 |
} |
| 296 |
/* |
| 297 |
========================================== |
| 298 |
Survey show sections count | End |
| 299 |
========================================== |
| 300 |
*/ |
| 301 |
|
| 302 |
/* |
| 303 |
========================================== |
| 304 |
Survey show questions count | Start |
| 305 |
========================================== |
| 306 |
*/ |
| 307 |
public function ays_generate_questions_count_method( $attr ){ |
| 308 |
|
| 309 |
$id = (isset($attr['id']) && $attr['id'] != '') ? absint( sanitize_text_field($attr['id']) ) : null; |
| 310 |
$this->default_texts = Survey_Maker_Data::ays_set_default_texts( $this->plugin_name, array() ); |
| 311 |
|
| 312 |
if (is_null($id) || $id == 0 ) { |
| 313 |
$survey_questions_count_html = "<p class='wrong_shortcode_text' style='color:red;'>" . $this->default_texts['wrongShortcode'] . "</p>"; |
| 314 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $survey_questions_count_html); |
| 315 |
} |
| 316 |
|
| 317 |
$unique_id = uniqid(); |
| 318 |
$this->unique_id = $unique_id; |
| 319 |
$this->unique_id_in_class = $id . "-" . $unique_id; |
| 320 |
|
| 321 |
|
| 322 |
$survey_questions_count_html = $this->ays_survey_questions_sections_count_html( $id , 'questions' ); |
| 323 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $survey_questions_count_html); |
| 324 |
} |
| 325 |
/* |
| 326 |
========================================== |
| 327 |
Survey show questions count | End |
| 328 |
========================================== |
| 329 |
*/ |
| 330 |
|
| 331 |
public function ays_generate_users_html($arg){ |
| 332 |
|
| 333 |
$results = $this->get_user_profile_data(); |
| 334 |
|
| 335 |
$content_html = array(); |
| 336 |
|
| 337 |
if( is_null( $results ) || $results == 0 ){ |
| 338 |
$content_html = ""; |
| 339 |
return $content_html; |
| 340 |
} |
| 341 |
|
| 342 |
$user_info = (isset( $results['user_'.$arg.'_name'] ) && $results['user_'.$arg.'_name'] != "") ? sanitize_text_field( $results['user_'.$arg.'_name'] ) : ''; |
| 343 |
|
| 344 |
$content_html[] = "<span class='ays-survey-user-".$arg."-name' id='ays-survey-user-".$arg."-name-". $this->unique_id_in_class ."' data-id='". $this->unique_id ."'>"; |
| 345 |
$content_html[] = $user_info; |
| 346 |
$content_html[] = "</span>"; |
| 347 |
|
| 348 |
$content_html = implode( '' , $content_html); |
| 349 |
|
| 350 |
return $content_html; |
| 351 |
} |
| 352 |
|
| 353 |
public function get_user_profile_data(){ |
| 354 |
|
| 355 |
$user_first_name = ''; |
| 356 |
$user_last_name = ''; |
| 357 |
$user_nickname = ''; |
| 358 |
|
| 359 |
$user_id = get_current_user_id(); |
| 360 |
if($user_id != 0){ |
| 361 |
$usermeta = get_user_meta( $user_id ); |
| 362 |
if($usermeta !== null){ |
| 363 |
$user_first_name = (isset($usermeta['first_name'][0]) && sanitize_text_field( $usermeta['first_name'][0] != '') ) ? sanitize_text_field( $usermeta['first_name'][0] ) : ''; |
| 364 |
$user_last_name = (isset($usermeta['last_name'][0]) && sanitize_text_field( $usermeta['last_name'][0] != '') ) ? sanitize_text_field( $usermeta['last_name'][0] ) : ''; |
| 365 |
$user_nickname = (isset($usermeta['nickname'][0]) && sanitize_text_field( $usermeta['nickname'][0] != '') ) ? sanitize_text_field( $usermeta['nickname'][0] ) : ''; |
| 366 |
} |
| 367 |
|
| 368 |
$current_user_data = get_userdata( $user_id ); |
| 369 |
if ( ! is_null( $current_user_data ) && $current_user_data ) { |
| 370 |
$user_display_name = ( isset( $current_user_data->data->display_name ) && $current_user_data->data->display_name != '' ) ? sanitize_text_field( $current_user_data->data->display_name ) : ""; |
| 371 |
$user_email = ( isset( $current_user_data->data->user_email ) && $current_user_data->data->user_email != '' ) ? sanitize_text_field( $current_user_data->data->user_email ) : ""; |
| 372 |
$user_wordpress_roles = ( isset( $current_user_data->roles ) && ! empty( $current_user_data->roles ) ) ? $current_user_data->roles : ""; |
| 373 |
if ( !empty( $user_wordpress_roles ) && $user_wordpress_roles != "" ) { |
| 374 |
if ( is_array( $user_wordpress_roles ) ) { |
| 375 |
$user_wordpress_roles = implode(",", $user_wordpress_roles); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
$message_data = array( |
| 382 |
'user_first_name' => $user_first_name, |
| 383 |
'user_last_name' => $user_last_name, |
| 384 |
'user_nick_name' => $user_nickname, |
| 385 |
'user_display_name' => $user_display_name, |
| 386 |
'user_email_name' => $user_email, |
| 387 |
'user_wordpress_roles_name' => $user_wordpress_roles, |
| 388 |
); |
| 389 |
|
| 390 |
return $message_data; |
| 391 |
} |
| 392 |
|
| 393 |
public function get_curent_survey_creation_date( $id ){ |
| 394 |
global $wpdb; |
| 395 |
|
| 396 |
$surveys_table = esc_sql( $wpdb->prefix . "ayssurvey_surveys" ); |
| 397 |
|
| 398 |
if (is_null($id) || $id == 0 ) { |
| 399 |
return null; |
| 400 |
} |
| 401 |
|
| 402 |
$id = absint( $id ); |
| 403 |
|
| 404 |
$sql = "SELECT DATE(`date_created`) FROM `{$surveys_table}` WHERE `id` = {$id}"; |
| 405 |
|
| 406 |
$results = $wpdb->get_var($sql); |
| 407 |
if ( is_null( $results ) || $results == "" ) { |
| 408 |
$results = null; |
| 409 |
} |
| 410 |
|
| 411 |
return sanitize_text_field( $results ); |
| 412 |
} |
| 413 |
|
| 414 |
public function ays_survey_questions_sections_count_html( $id , $detect ){ |
| 415 |
$detector = "get_survey_".$detect."_count"; |
| 416 |
$results = Survey_Maker_Data::$detector($id); |
| 417 |
|
| 418 |
$content_html = array(); |
| 419 |
|
| 420 |
if($results === null || $results === 0){ |
| 421 |
$content_html = ""; |
| 422 |
return $content_html; |
| 423 |
} |
| 424 |
|
| 425 |
$content_html[] = "<span class='". $this->html_name_prefix.$detect."-count-box' id='". $this->html_name_prefix.$detect."-count-box-". $this->unique_id_in_class ."' data-id='". $this->unique_id ."'>"; |
| 426 |
$content_html[] = $results; |
| 427 |
$content_html[] = "</span>"; |
| 428 |
|
| 429 |
$content_html = implode( '' , $content_html); |
| 430 |
|
| 431 |
return $content_html; |
| 432 |
} |
| 433 |
|
| 434 |
/* |
| 435 |
========================================== |
| 436 |
Show users lastname | Start |
| 437 |
========================================== |
| 438 |
*/ |
| 439 |
public function ays_generate_categories_count_method(){ |
| 440 |
|
| 441 |
$unique_id = uniqid(); |
| 442 |
$this->unique_id = $unique_id; |
| 443 |
$this->unique_id_in_class = $unique_id; |
| 444 |
|
| 445 |
$categories_count_html = ""; |
| 446 |
$categories_count_html = $this->ays_survey_categories_count_html(); |
| 447 |
|
| 448 |
return str_replace(array("\r\n", "\n", "\r"), "\n", $categories_count_html); |
| 449 |
} |
| 450 |
|
| 451 |
public function ays_survey_categories_count_html(){ |
| 452 |
|
| 453 |
$results = $this->ays_survey_get_categories_count(); |
| 454 |
$content_html = array(); |
| 455 |
|
| 456 |
if( is_null($results) || $results == 0){ |
| 457 |
$content_html = "<p style='text-align: center;font-style:italic;'>" . __( "There are no results yet.", "survey-maker" ) . "</p>"; |
| 458 |
return $content_html; |
| 459 |
} |
| 460 |
|
| 461 |
$results_count = (isset( $results ) && $results != '') ? sanitize_text_field( $results ) : 0; |
| 462 |
|
| 463 |
if ( $results_count == 0 ) { |
| 464 |
$content_html = ""; |
| 465 |
return $content_html; |
| 466 |
} |
| 467 |
|
| 468 |
$content_html[] = "<span class='". $this->html_name_prefix ."categories-count' id='". $this->html_name_prefix ."categories-". $this->unique_id_in_class ."' data-id='". $this->unique_id ."'>"; |
| 469 |
$content_html[] = $results_count; |
| 470 |
$content_html[] = "</span>"; |
| 471 |
|
| 472 |
$content_html = implode( '' , $content_html); |
| 473 |
|
| 474 |
return $content_html; |
| 475 |
} |
| 476 |
|
| 477 |
public static function ays_survey_get_categories_count(){ |
| 478 |
global $wpdb; |
| 479 |
$surveys_category_table = esc_sql( $wpdb->prefix . "ayssurvey_survey_categories" ); |
| 480 |
|
| 481 |
$sql = "SELECT count(*) as res_count FROM {$surveys_category_table}"; |
| 482 |
|
| 483 |
$res = $wpdb->get_var($sql); |
| 484 |
|
| 485 |
return $res; |
| 486 |
} |
| 487 |
/* |
| 488 |
========================================== |
| 489 |
Show users lastname | End |
| 490 |
========================================== |
| 491 |
*/ |
| 492 |
} |
| 493 |
|