| 1 |
<?php |
| 2 |
/** |
| 3 |
* The public-facing functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://ays-pro.com/ |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Poll_Maker |
| 9 |
* @subpackage Poll_Maker/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The public-facing functionality of the plugin. |
| 14 |
* |
| 15 |
* Defines the plugin name, version, and two examples hooks for how to |
| 16 |
* enqueue the public-facing stylesheet and JavaScript. |
| 17 |
* |
| 18 |
* @package Poll_Maker |
| 19 |
* @subpackage Poll_Maker/includes |
| 20 |
* @author AYS Pro LLC <[email protected]> |
| 21 |
*/ |
| 22 |
class Poll_Maker_Data { |
| 23 |
|
| 24 |
// Retrieves the attachment ID from the file URL |
| 25 |
public static function ays_poll_get_image_id_by_url( $image_url ) { |
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
$image_alt_text = ""; |
| 29 |
if ( !empty( $image_url ) ) { |
| 30 |
|
| 31 |
$re = '/-\d+[Xx]\d+\./'; |
| 32 |
$subst = '.'; |
| 33 |
|
| 34 |
$image_url = preg_replace($re, $subst, $image_url, 1); |
| 35 |
|
| 36 |
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); |
| 37 |
if ( !is_null( $attachment ) && !empty( $attachment ) ) { |
| 38 |
|
| 39 |
$image_id = (isset( $attachment[0] ) && $attachment[0] != "") ? absint( $attachment[0] ) : ""; |
| 40 |
if ( $image_id != "" ) { |
| 41 |
$image_alt_text = self::ays_poll_get_image_alt_text_by_id( $image_id ); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $image_alt_text; |
| 47 |
} |
| 48 |
|
| 49 |
public static function ays_poll_get_image_alt_text_by_id( $image_id ) { |
| 50 |
|
| 51 |
$image_data = ""; |
| 52 |
if ( $image_id != "" ) { |
| 53 |
|
| 54 |
$result = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); |
| 55 |
if ( $result && $result != "" ) { |
| 56 |
$image_data = esc_attr( $result ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return $image_data; |
| 61 |
} |
| 62 |
|
| 63 |
public static function ays_poll_autoembed( $content ) { |
| 64 |
global $wp_embed; |
| 65 |
$content = stripslashes( wpautop( $content ) ); |
| 66 |
$content = $wp_embed->autoembed( $content ); |
| 67 |
if ( strpos( $content, '[embed]' ) !== false ) { |
| 68 |
$content = $wp_embed->run_shortcode( $content ); |
| 69 |
} |
| 70 |
$content = do_shortcode( $content ); |
| 71 |
return $content; |
| 72 |
} |
| 73 |
|
| 74 |
public static function ays_poll_is_elementor(){ |
| 75 |
if( isset( $_GET['action'] ) && $_GET['action'] == 'elementor' ){ |
| 76 |
$is_elementor = true; |
| 77 |
}elseif( isset( $_REQUEST['elementor-preview'] ) && $_REQUEST['elementor-preview'] != '' ){ |
| 78 |
$is_elementor = true; |
| 79 |
}else{ |
| 80 |
$is_elementor = false; |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! $is_elementor ) { |
| 84 |
$is_elementor = ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ? true : false; |
| 85 |
} |
| 86 |
|
| 87 |
return $is_elementor; |
| 88 |
} |
| 89 |
|
| 90 |
public static function ays_poll_is_editor(){ |
| 91 |
$is_editor = false; |
| 92 |
if( isset( $_GET['action'] ) && ( $_GET['action'] == 'add' || $_GET['action'] == 'edit' ) ){ |
| 93 |
if ( isset( $_GET['post'] ) && absint( $_GET['post'] ) > 0 ) { |
| 94 |
$is_editor = true; |
| 95 |
} |
| 96 |
} elseif ( isset( $_GET['context'] ) && ( $_GET['context'] == 'add' || $_GET['context'] == 'edit' ) ) { |
| 97 |
if( isset( $_GET['post_id'] ) & absint( $_GET['post_id'] ) > 0 ){ |
| 98 |
$is_editor = true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $is_editor; |
| 103 |
} |
| 104 |
|
| 105 |
public static function get_user_passed_polls_count( $user_id ){ |
| 106 |
global $wpdb; |
| 107 |
|
| 108 |
if (is_null($user_id) || $user_id == 0 ) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
$user_id = absint( $user_id ); |
| 113 |
|
| 114 |
$reports_table = esc_sql($wpdb->prefix."ayspoll_reports"); |
| 115 |
$answ_table = esc_sql($wpdb->prefix."ayspoll_answers"); |
| 116 |
$polls_table = esc_sql($wpdb->prefix."ayspoll_polls"); |
| 117 |
|
| 118 |
$sql = "SELECT COUNT(*) FROM {$reports_table} AS r |
| 119 |
JOIN {$answ_table} AS a |
| 120 |
ON a.id = r.answer_id |
| 121 |
JOIN {$polls_table} AS p |
| 122 |
ON a.poll_id = p.id |
| 123 |
WHERE r.user_id = ".$user_id; |
| 124 |
|
| 125 |
$results = $wpdb->get_var($sql); |
| 126 |
|
| 127 |
if ( ! empty( $results ) ) { |
| 128 |
$results = absint( $results ); |
| 129 |
} else { |
| 130 |
$results = 0; |
| 131 |
} |
| 132 |
|
| 133 |
return $results; |
| 134 |
} |
| 135 |
|
| 136 |
public static function get_all_polls(){ |
| 137 |
global $wpdb; |
| 138 |
$polls_table = $wpdb->prefix."ayspoll_polls"; |
| 139 |
$sql = "SELECT id,title FROM ".$polls_table; |
| 140 |
$result = $wpdb->get_results($sql , "ARRAY_A"); |
| 141 |
return $result; |
| 142 |
} |
| 143 |
|
| 144 |
public static function check_user_capability(){ |
| 145 |
return current_user_can( 'manage_options' ) && is_user_logged_in(); |
| 146 |
} |
| 147 |
} |