WP init
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Actions_Status setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Actions_Status {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_update_form_action_meta', [$this, 'wpr_update_form_action_meta']);
|
||||
add_action('wp_ajax_nopriv_wpr_update_form_action_meta', [$this, 'wpr_update_form_action_meta']);
|
||||
}
|
||||
|
||||
// In your PHP file
|
||||
public function wpr_update_form_action_meta() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
// Validate custom token
|
||||
// $custom_token = $_POST['custom_token'];
|
||||
|
||||
// if ( is_user_logged_in() ) {
|
||||
// // For logged-in users, validate against their user ID
|
||||
// $user_id = get_current_user_id();
|
||||
// $stored_token = get_transient( 'wpr_custom_token_' . $user_id );
|
||||
// } else {
|
||||
// // For non-logged-in users, use the guest token from the cookie
|
||||
// if ( isset( $_COOKIE['wpr_guest_token'] ) ) {
|
||||
// $guest_id = sanitize_text_field( $_COOKIE['wpr_guest_token'] );
|
||||
// $stored_token = get_transient( 'wpr_custom_guest_token_' . $guest_id );
|
||||
// } else {
|
||||
// wp_send_json_error( 'Invalid token.' );
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if ( ! $stored_token || $custom_token !== $stored_token ) {
|
||||
// wp_send_json_error( 'Invalid token.' );
|
||||
// return;
|
||||
// }
|
||||
|
||||
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
|
||||
$action_name = isset($_POST['action_name']) ? sanitize_text_field($_POST['action_name']) : '';
|
||||
$status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : '';
|
||||
$message = isset($_POST['message']) ? sanitize_text_field($_POST['message']) : '';
|
||||
|
||||
$meta_value = [
|
||||
'status' => $status,
|
||||
'message' => $message
|
||||
];
|
||||
|
||||
$actions_whitelist = [
|
||||
'wpr_form_builder_email',
|
||||
'wpr_form_builder_submissions',
|
||||
'wpr_form_builder_mailchimp',
|
||||
'wpr_form_builder_webhook'
|
||||
];
|
||||
|
||||
if ($post_id && $action_name && $status && in_array($action_name, $actions_whitelist)) {
|
||||
update_post_meta($post_id, '_action_' . $action_name, $meta_value);
|
||||
wp_send_json_success('Post meta updated successfully');
|
||||
} else {
|
||||
wp_send_json_error('Invalid data provided');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Actions_Status();
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Send_Email setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_File_Upload {
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_addons_upload_file', [$this, 'handle_file_upload']);
|
||||
add_action('wp_ajax_nopriv_wpr_addons_upload_file', [$this, 'handle_file_upload']);
|
||||
}
|
||||
|
||||
public function handle_file_upload() {
|
||||
// $email_fields = get_option('wpr_email_fields_' . $_POST['wpr_form_id']); remove if necessary
|
||||
|
||||
if (!isset($_POST['wpr_addons_nonce']) || !wp_verify_nonce($_POST['wpr_addons_nonce'], 'wpr-addons-js')) {
|
||||
wp_send_json_error(array(
|
||||
'message' => esc_html__('Security check failed.', 'wpr-addons'),
|
||||
));
|
||||
}
|
||||
|
||||
if ( !isset($_POST['form_field_id']) || !get_option( 'wpr_form_upload_field_in_use_' . sanitize_text_field( $_POST['form_field_id'] ) ) ) {
|
||||
wp_send_json_error(['message' => 'Permission denied'], 403);
|
||||
}
|
||||
|
||||
// Get the max_file_size value from the file_size control.
|
||||
$max_file_size = isset($_POST['max_file_size']) ? floatval(sanitize_text_field($_POST['max_file_size'])) : 0; // Replace this with the value from the file_size control.
|
||||
if ($max_file_size <= 0) {
|
||||
$max_file_size = wp_max_upload_size() / pow(1024, 2); //MB
|
||||
}
|
||||
|
||||
// Check if a file is uploaded.
|
||||
if (isset($_FILES['uploaded_file'])) {
|
||||
$file = $_FILES['uploaded_file'];
|
||||
|
||||
// Check if the uploaded file size exceeds the allowed limit.
|
||||
if ($file['size'] > $max_file_size * 1024 * 1024) {
|
||||
wp_send_json_error(array(
|
||||
'cause' => 'filesize',
|
||||
'sizes' => [
|
||||
$max_file_size * 1024 * 1024,
|
||||
$file['size']
|
||||
],
|
||||
'message' => 'File size exceeds the allowed limit.'
|
||||
));
|
||||
}
|
||||
|
||||
// valid file type?
|
||||
if ( !$this->file_validity( $file ) ) {
|
||||
wp_send_json_error(array(
|
||||
'cause' => 'filetype',
|
||||
'message' => esc_html__('File type is not valid.', 'wpr-addons')
|
||||
));
|
||||
}
|
||||
|
||||
if ( 'click' == $_POST['triggering_event'] ) {
|
||||
// Set the upload directory.
|
||||
$upload_dir = wp_upload_dir();
|
||||
$upload_path = $upload_dir['basedir'] . '/wpr-addons/forms';
|
||||
|
||||
wp_mkdir_p( $upload_path );
|
||||
|
||||
// Generate a unique file name.
|
||||
$filename = wp_unique_filename($upload_path, $file['name']);
|
||||
|
||||
// Move the uploaded file to the uploads directory.
|
||||
if (move_uploaded_file($file['tmp_name'], $upload_path . '/' . $filename)) {
|
||||
// Return the uploaded file's URL.
|
||||
wp_send_json_success(array(
|
||||
'url' => $upload_dir['baseurl'] . '/wpr-addons/forms/' . $filename
|
||||
));
|
||||
} else {
|
||||
wp_send_json_error(array(
|
||||
'message' => esc_html__('Failed to upload the file.', 'wpr-addons')
|
||||
));
|
||||
}
|
||||
} else {
|
||||
wp_send_json_success(array(
|
||||
'message' => esc_html__('File validation passed', 'wpr-addons')
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'click' == $_POST['triggering_event'] ) {
|
||||
// Folder not being created during interaction was causing issues
|
||||
$upload_dir = wp_upload_dir();
|
||||
$upload_path = $upload_dir['basedir'] . '/wpr-addons/forms';
|
||||
|
||||
wp_mkdir_p( $upload_path );
|
||||
|
||||
wp_send_json_error(array(
|
||||
'message' => esc_html__('No file was uploaded.', 'wpr-addons'),
|
||||
'files' => $_FILES['uploaded_file']
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// private function file_validity( $field, $file ) {
|
||||
private function file_validity( $file ) {
|
||||
$whitelist = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'odt', 'avi', 'ogg', 'm4a', 'mov', 'mp3', 'mp4', 'mpg', 'wav', 'wmv', 'txt'];
|
||||
|
||||
// File type validation
|
||||
if ( empty( $_POST['allowed_file_types'] ) ) {
|
||||
$allowed_file_types = 'jpg,jpeg,png,gif,pdf,doc,docx,ppt,pptx,odt,avi,ogg,m4a,mov,mp3,mp4,mpg,wav,wmv,txt';
|
||||
} else {
|
||||
$allowed_file_types = $_POST['allowed_file_types'];
|
||||
}
|
||||
|
||||
if (!wp_check_filetype($file['name'])['ext']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$f_extension = pathinfo( $file['name'], PATHINFO_EXTENSION );
|
||||
$f_extension = strtolower( $f_extension );
|
||||
|
||||
$allowed_file_types = explode( ',', $allowed_file_types );
|
||||
$allowed_file_types = array_map( 'trim', $allowed_file_types );
|
||||
$allowed_file_types = array_map( 'strtolower', $allowed_file_types );
|
||||
|
||||
return ( in_array( $f_extension, $allowed_file_types ) && in_array( $f_extension, $whitelist ) && !in_array( $f_extension, $this->get_exclusion_list() ) );
|
||||
}
|
||||
|
||||
private function get_exclusion_list() {
|
||||
static $exclusionlist = false;
|
||||
if ( ! $exclusionlist ) {
|
||||
$exclusionlist = [
|
||||
'php',
|
||||
'php3',
|
||||
'php4',
|
||||
'php5',
|
||||
'php6',
|
||||
'phps',
|
||||
'php7',
|
||||
'phtml',
|
||||
'shtml',
|
||||
'pht',
|
||||
'swf',
|
||||
'html',
|
||||
'asp',
|
||||
'aspx',
|
||||
'cmd',
|
||||
'csh',
|
||||
'bat',
|
||||
'htm',
|
||||
'hta',
|
||||
'jar',
|
||||
'exe',
|
||||
'com',
|
||||
'js',
|
||||
'lnk',
|
||||
'htaccess',
|
||||
'htpasswd',
|
||||
'phtml',
|
||||
'ps1',
|
||||
'ps2',
|
||||
'py',
|
||||
'rb',
|
||||
'tmp',
|
||||
'cgi',
|
||||
'svg',
|
||||
'svgz'
|
||||
];
|
||||
}
|
||||
|
||||
return $exclusionlist;
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_File_Upload();
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Actions_Status setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Recaptcha_Handler {
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_verify_recaptcha', [$this, 'wpr_verify_recaptcha']);
|
||||
add_action('wp_ajax_nopriv_wpr_verify_recaptcha', [$this, 'wpr_verify_recaptcha']);
|
||||
}
|
||||
|
||||
// In your PHP file
|
||||
public function wpr_verify_recaptcha() {
|
||||
$recaptcha_response = $_POST['g-recaptcha-response'];
|
||||
$is_valid_recaptcha = $this->check_recaptcha($recaptcha_response);
|
||||
|
||||
if ($is_valid_recaptcha[0] && $is_valid_recaptcha[1] >= get_option('wpr_recaptcha_v3_score')) {
|
||||
// Proceed with form processing
|
||||
wp_send_json_success(array(
|
||||
'message' => 'Recaptcha Success',
|
||||
'score' => $is_valid_recaptcha[1]
|
||||
));
|
||||
} else {
|
||||
// Handle the invalid reCAPTCHA case
|
||||
wp_send_json_error(array(
|
||||
'message' => 'Recaptcha Error',
|
||||
'score' => $is_valid_recaptcha[1],
|
||||
'results' => [
|
||||
$is_valid_recaptcha[0],
|
||||
$is_valid_recaptcha[1] >= get_option('wpr_recaptcha_v3_score')
|
||||
]
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function check_recaptcha($recaptcha_response) {
|
||||
$secret_key = get_option('wpr_recaptcha_v3_secret_key');
|
||||
$remote_ip = $_SERVER['REMOTE_ADDR'];
|
||||
|
||||
$response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
|
||||
'body' => array(
|
||||
'secret' => $secret_key,
|
||||
'response' => $recaptcha_response,
|
||||
'remoteip' => $remote_ip
|
||||
)
|
||||
));
|
||||
|
||||
if (is_wp_error($response)) {
|
||||
// Handle the error accordingly
|
||||
return false;
|
||||
}
|
||||
|
||||
$decoded_response = json_decode(wp_remote_retrieve_body($response), true);
|
||||
|
||||
$score = $decoded_response['score'];
|
||||
|
||||
if ($decoded_response['success'] === true) {
|
||||
// reCAPTCHA verification passed
|
||||
return [true, $score];
|
||||
} else {
|
||||
// reCAPTCHA verification failed
|
||||
return [false, $score];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Recaptcha_Handler();
|
||||
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Send_Email setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Send_Email {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_form_builder_email' , [$this, 'send_email']);
|
||||
add_action('wp_ajax_nopriv_wpr_form_builder_email',[$this, 'send_email']);
|
||||
}
|
||||
|
||||
public function send_email() {
|
||||
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
$message_body = [];
|
||||
|
||||
foreach ($_POST['form_content'] as $field) {
|
||||
if ($field[0] === 'email') {
|
||||
if (!is_email($field[1])) {
|
||||
// The field is an email, but it is not a valid email address
|
||||
// Take action or abort function execution here
|
||||
wp_send_json_error(array(
|
||||
'action' => 'wpr_form_builder_email',
|
||||
'message' => esc_html__('Email provided is invalid', 'wpr-addons'),
|
||||
'status' => 'error'
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rest of your function code here (if needed)
|
||||
$content_type = get_option('wpr_email_content_type_'. $_POST['wpr_form_id']);
|
||||
|
||||
$line_break = 'html' === $content_type ? '<br>' : "\n";
|
||||
|
||||
$email_fields = trim(get_option('wpr_email_fields_' . $_POST['wpr_form_id']));
|
||||
|
||||
if ( $email_fields === '[all-fields]' || str_contains($email_fields, '[all-fields]') ) {
|
||||
|
||||
// Function to replace the shortcode with the form value
|
||||
$replace_shortcode_with_value = function ($matches) {
|
||||
$field_id = $matches[1];
|
||||
foreach ($_POST['form_content'] as $key => $value) {
|
||||
$key_parts = explode('-', $key);
|
||||
$last_part = end($key_parts);
|
||||
if ($last_part === $field_id) {
|
||||
return is_array($value[1]) ? implode("\n", $value[1]) : $value[1];
|
||||
}
|
||||
}
|
||||
return ''; // Return an empty string if the field id is not found
|
||||
};
|
||||
|
||||
// Function to replace the shortcode with the form value
|
||||
$all_fields_content = [];
|
||||
|
||||
foreach ($_POST['form_content'] as $key => $value) {
|
||||
$all_fields_content[] = is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
|
||||
}
|
||||
$all_fields_content = implode("\n", $all_fields_content);
|
||||
|
||||
// Process user input to replace shortcodes
|
||||
$processed_message = str_replace('[all-fields]', $all_fields_content, $email_fields);
|
||||
|
||||
$processed_message = preg_replace_callback(
|
||||
'/\[id="([^"]+)"\]/',
|
||||
$replace_shortcode_with_value,
|
||||
$processed_message
|
||||
);
|
||||
} else {
|
||||
|
||||
// Function to replace the shortcode with the form value
|
||||
$replace_shortcode_with_value = function ($matches) {
|
||||
$field_id = $matches[1];
|
||||
foreach ($_POST['form_content'] as $key => $value) {
|
||||
$key_parts = explode('-', $key);
|
||||
$last_part = end($key_parts);
|
||||
if ($last_part === $field_id) {
|
||||
return is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
|
||||
}
|
||||
}
|
||||
return ''; // Return an empty string if the field id is not found
|
||||
};
|
||||
|
||||
// Process user input to replace shortcodes
|
||||
$processed_message = preg_replace_callback(
|
||||
'/\[id="([^"]+)"\]/',
|
||||
$replace_shortcode_with_value,
|
||||
$email_fields
|
||||
);
|
||||
}
|
||||
|
||||
$meta_keys = get_option('wpr_meta_keys_'. $_POST['wpr_form_id']);
|
||||
$meta_fields = [];
|
||||
|
||||
foreach ( $meta_keys as $metadata_type ) {
|
||||
switch ( $metadata_type ) {
|
||||
case 'date':
|
||||
$meta_fields['date'] = [
|
||||
'title' => esc_html__( 'Date', 'wpr-addons' ),
|
||||
'value' => date_i18n( get_option( 'date_format' ) ),
|
||||
];
|
||||
break;
|
||||
|
||||
case 'time':
|
||||
$meta_fields['time'] = [
|
||||
'title' => esc_html__( 'Time', 'wpr-addons' ),
|
||||
'value' => date_i18n( get_option( 'time_format' ) ),
|
||||
];
|
||||
break;
|
||||
|
||||
case 'page_url':
|
||||
$meta_fields['page_url'] = [
|
||||
'title' => esc_html__( 'Page URL', 'wpr-addons' ),
|
||||
// 'value' => get_option('wpr_referrer_'. $_POST['wpr_form_id']) ? esc_url_raw( wp_unslash( get_option('wpr_referrer_'. $_POST['wpr_form_id']) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
'value' => get_option('wpr_referrer_'. $_POST['wpr_form_id']) ? get_option('wpr_referrer_'. $_POST['wpr_form_id']) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
];
|
||||
break;
|
||||
|
||||
case 'page_title':
|
||||
$meta_fields['page_title'] = [
|
||||
'title' => esc_html__( 'Page Title', 'wpr-addons' ),
|
||||
// 'value' => get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ? sanitize_text_field( wp_unslash( get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
'value' => get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ? get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
];
|
||||
break;
|
||||
|
||||
case 'user_agent':
|
||||
$meta_fields['user_agent'] = [
|
||||
'title' => esc_html__( 'User Agent', 'wpr-addons' ),
|
||||
'value' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_textarea_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
|
||||
];
|
||||
break;
|
||||
|
||||
case 'remote_ip':
|
||||
$meta_fields['remote_ip'] = [
|
||||
'title' => esc_html__( 'Remote IP', 'wpr-addons' ),
|
||||
'value' => Utilities::get_client_ip(),
|
||||
];
|
||||
break;
|
||||
|
||||
case 'credit':
|
||||
$meta_fields['credit'] = [
|
||||
'title' => esc_html__( 'Powered by', 'wpr-addons' ),
|
||||
'value' => esc_html__( 'Royal Addons', 'wpr-addons' ), // is it necessary ?
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$email_meta = [];
|
||||
|
||||
foreach( $meta_fields as $key => $value ) {
|
||||
$email_meta[] = $value['title'] . ': ' . $value['value'];
|
||||
}
|
||||
|
||||
$to = get_option('wpr_email_to_'. $_POST['wpr_form_id']);
|
||||
|
||||
$to = preg_replace_callback(
|
||||
'/\[id="(\w+)"\]/',
|
||||
function ($matches) {
|
||||
return $this->get_field_value($matches[1]);
|
||||
},
|
||||
$to
|
||||
);
|
||||
|
||||
$subject = get_option('wpr_email_subject_'. $_POST['wpr_form_id']);
|
||||
|
||||
$subject = preg_replace_callback(
|
||||
'/\[id="(\w+)"\]/',
|
||||
function ($matches) {
|
||||
return $this->get_field_value($matches[1]);
|
||||
},
|
||||
$subject
|
||||
);
|
||||
|
||||
if ( $processed_message ) {
|
||||
$message_body[] = $processed_message;
|
||||
}
|
||||
|
||||
// Prepare the message body with correct line breaks
|
||||
if ($content_type === 'html') {
|
||||
// Replace all instances of \n with <br> in each message body item
|
||||
foreach ($message_body as &$item) {
|
||||
$item = nl2br($item);
|
||||
}
|
||||
unset($item); // Break the reference with the last element
|
||||
}
|
||||
|
||||
$body = implode($line_break, $message_body) . $line_break . '-----' . $line_break . implode($line_break, $email_meta);
|
||||
|
||||
$cc_header = '';
|
||||
if ( !empty( get_option('wpr_cc_header_'. $_POST['wpr_form_id']) ) ) {
|
||||
$cc_header = 'Cc: ' . get_option('wpr_cc_header_'. $_POST['wpr_form_id']);
|
||||
|
||||
$cc_header = preg_replace_callback(
|
||||
'/\[id="(\w+)"\]/',
|
||||
function ($matches) {
|
||||
return $this->get_field_value($matches[1]);
|
||||
},
|
||||
$cc_header
|
||||
);
|
||||
}
|
||||
|
||||
$bcc_header = '';
|
||||
if ( !empty( get_option('wpr_bcc_header_'. $_POST['wpr_form_id']) ) ) {
|
||||
$bcc_header = 'Bcc: ' . get_option('wpr_bcc_header_'. $_POST['wpr_form_id']);
|
||||
|
||||
$bcc_header = preg_replace_callback(
|
||||
'/\[id="(\w+)"\]/',
|
||||
function ($matches) {
|
||||
return $this->get_field_value($matches[1]);
|
||||
},
|
||||
$bcc_header
|
||||
);
|
||||
}
|
||||
|
||||
if ( !empty( get_option('wpr_reply_to_'. $_POST['wpr_form_id']) ) && !empty(get_option('wpr_email_from_name_'. $_POST['wpr_form_id'])) && !empty(get_option('wpr_email_from_'. $_POST['wpr_form_id'])) ) {
|
||||
|
||||
preg_match_all('/id="([^"]+)"/', get_option('wpr_reply_to_'. $_POST['wpr_form_id']), $matche);
|
||||
$reply_to_field_id = $matche[1];
|
||||
|
||||
preg_match_all('/id="([^"]+)"/', get_option('wpr_email_from_name_'. $_POST['wpr_form_id']), $matche);
|
||||
$email_from_name_field_id = $matche[1];
|
||||
|
||||
preg_match_all('/id="([^"]+)"/', get_option('wpr_email_from_'. $_POST['wpr_form_id']), $matche);
|
||||
$email_from_field_id = $matche[1];
|
||||
|
||||
foreach ( $_POST['form_content'] as $key => $value ) {
|
||||
$key_parts = explode('-', $key);
|
||||
$last_part = end($key_parts);
|
||||
|
||||
if (in_array($last_part, $reply_to_field_id)) {
|
||||
$reply_to_address = $value[1];
|
||||
}
|
||||
|
||||
if (in_array($last_part, $email_from_name_field_id)) {
|
||||
$email_from_name = $value[1];
|
||||
}
|
||||
|
||||
if (in_array($last_part, $email_from_field_id)) {
|
||||
$email_from_mail = $value[1];
|
||||
}
|
||||
}
|
||||
|
||||
if ( !isset($reply_to_address) || empty($reply_to_address) ) {
|
||||
$reply_to_address = get_option('wpr_reply_to_'. $_POST['wpr_form_id']);
|
||||
}
|
||||
|
||||
if ( !isset($email_from_name) || empty($email_from_name) ) {
|
||||
$email_from_name = get_option('wpr_email_from_name_'. $_POST['wpr_form_id']);
|
||||
}
|
||||
|
||||
if ( !isset($email_from_mail) || empty($email_from_mail) ) {
|
||||
$email_from_mail = get_option('wpr_email_from_'. $_POST['wpr_form_id']);
|
||||
}
|
||||
|
||||
$reply_to = 'Reply-To: ' . $reply_to_address;
|
||||
}
|
||||
|
||||
$email_from = sprintf( 'From: %s <%s>' . "\r\n", $email_from_name, $email_from_mail );
|
||||
|
||||
$headers = array('Content-Type: text/'. $content_type .'; charset=UTF-8', $email_from, $cc_header, $bcc_header, $reply_to);
|
||||
|
||||
// Send email using wp_mail() function
|
||||
$sent = wp_mail( $to, $subject, $body, $headers);
|
||||
|
||||
if ( $sent ) {
|
||||
wp_send_json_success(array(
|
||||
'action' => 'wpr_form_builder_email',
|
||||
'message' => esc_html__('Message sent successfully', 'wpr-addons'),
|
||||
'status' => 'success',
|
||||
'details' => json_encode($message_body)
|
||||
));
|
||||
} else {
|
||||
wp_send_json_error(array(
|
||||
'action' => 'wpr_form_builder_email',
|
||||
'message' => esc_html__('Message could not be sent', 'wpr-addons'),
|
||||
'status' => 'error',
|
||||
'details' => json_encode($message_body)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function get_field_value($field_id) {
|
||||
foreach ($_POST['form_content'] as $key => $field) {
|
||||
$key_parts = explode('-', $key);
|
||||
$last_part = end($key_parts);
|
||||
|
||||
if ($last_part === $field_id) {
|
||||
return $field[1];
|
||||
}
|
||||
}
|
||||
return ''; // Return an empty string if the field id is not found
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Send_Email();
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Send_Webhook setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Send_Webhook {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_form_builder_webhook' , [$this, 'send_webhook']);
|
||||
add_action('wp_ajax_nopriv_wpr_form_builder_webhook',[$this, 'send_webhook']);
|
||||
}
|
||||
|
||||
public function send_webhook() {
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
$message_body = [];
|
||||
|
||||
foreach ( $_POST['form_content'] as $key => $value ) {
|
||||
if ( is_array($value[1]) ) {
|
||||
if ( empty($value[2]) ) {
|
||||
$message_body[trim($key)] = implode("\n", $value[1]);
|
||||
} else {
|
||||
$message_body[trim($value[2])] = implode("\n", $value[1]);
|
||||
}
|
||||
} else {
|
||||
if ( empty($value2) ) {
|
||||
$message_body[trim($key)] = $value[1];
|
||||
} else {
|
||||
$message_body[trim($value[2])] = $value[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$message_body['form_id'] = $_POST['wpr_form_id'];
|
||||
$message_body['form_name'] = $_POST['form_name'];
|
||||
|
||||
$args = [
|
||||
'body' => $message_body,
|
||||
];
|
||||
|
||||
$response = wp_remote_post( trim(get_option('wpr_webhook_url_' . $_POST['wpr_form_id'])), $args );
|
||||
|
||||
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
|
||||
wp_send_json_error(array(
|
||||
'action' => 'wpr_form_builder_webhook',
|
||||
'message' => esc_html__('Webhook error', 'wpr-addons'),
|
||||
'status' => 'error',
|
||||
'details' => json_encode($message_body)
|
||||
));
|
||||
// throw new \Exception( 'Webhook error.' );
|
||||
} else {
|
||||
wp_send_json_success(array(
|
||||
'action' => 'wpr_form_builder_webhook',
|
||||
'message' => esc_html__('Webhook success', 'wpr-addons'),
|
||||
'status' => 'success',
|
||||
'details' => json_encode($message_body)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Send_Webhook();
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use Elementor\Group_Control_Image_Size;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Form_Builder_Submissions setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Form_Builder_Submissions {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_form_builder_submissions' , [$this, 'add_to_submissions']);
|
||||
add_action('wp_ajax_nopriv_wpr_form_builder_submissions',[$this, 'add_to_submissions']);
|
||||
add_action('save_post', [$this, 'update_submissions_post_meta']);
|
||||
}
|
||||
|
||||
public function add_to_submissions() {
|
||||
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
$new = [
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'wpr_submissions'
|
||||
];
|
||||
|
||||
$post_id = wp_insert_post( $new );
|
||||
foreach ($_POST['form_content'] as $key => $value ) {
|
||||
update_post_meta($post_id, $key, [$value[0], $value[1], $value[2]]);
|
||||
}
|
||||
|
||||
$sanitized_form_name = sanitize_text_field($_POST['form_name']);
|
||||
$sanitized_form_id = sanitize_text_field($_POST['form_id']);
|
||||
$sanitized_form_page = sanitize_text_field($_POST['form_page']);
|
||||
$sanitized_form_page_id = sanitize_text_field($_POST['form_page_id']);
|
||||
|
||||
update_post_meta($post_id, 'wpr_form_name', $sanitized_form_name);
|
||||
update_post_meta($post_id, 'wpr_form_id', $sanitized_form_id);
|
||||
update_post_meta($post_id, 'wpr_form_page', $sanitized_form_page);
|
||||
update_post_meta($post_id, 'wpr_form_page_id', $sanitized_form_page_id);
|
||||
update_post_meta($post_id, 'wpr_user_agent', sanitize_textarea_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ));
|
||||
update_post_meta($post_id, 'wpr_user_ip', Utilities::get_client_ip());
|
||||
|
||||
if( $post_id ) {
|
||||
wp_send_json_success(array(
|
||||
'action' => 'wpr_form_builder_submissions',
|
||||
'post_id' => $post_id,
|
||||
'message' => esc_html__('Submission created successfully', 'wpr-addons'),
|
||||
'status' => 'success',
|
||||
'content' => $_POST['form_content']
|
||||
));
|
||||
} else {
|
||||
wp_send_json_success(array(
|
||||
'action' => 'wpr_form_builder_submissions',
|
||||
'post_id' => $post_id,
|
||||
'message' => esc_html__('Submit action failed', 'wpr-addons'),
|
||||
'status' => 'error'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function update_submissions_post_meta($post_id) {
|
||||
// Only allow users with edit permissions
|
||||
if ( ! current_user_can('edit_post', $post_id) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset($_POST['wpr_submission_changes']) && ! empty($_POST['wpr_submission_changes']) ) {
|
||||
$changes = json_decode(stripslashes($_POST['wpr_submission_changes']), true);
|
||||
|
||||
if ( ! is_array($changes) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// List of disallowed meta keys
|
||||
$disallowed_keys = [
|
||||
'_elementor_data',
|
||||
'_elementor_controls_usage',
|
||||
'_wp_attached_file',
|
||||
// Add more if needed
|
||||
];
|
||||
|
||||
foreach ( $changes as $key => $value ) {
|
||||
// Skip blacklisted keys
|
||||
if ( in_array($key, $disallowed_keys, true) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sanitize key and value
|
||||
$safe_key = sanitize_key($key);
|
||||
|
||||
if ( is_string($value) ) {
|
||||
$safe_value = sanitize_text_field($value);
|
||||
} elseif ( is_array($value) ) {
|
||||
$safe_value = array_map('sanitize_text_field', $value);
|
||||
} else {
|
||||
$safe_value = sanitize_text_field((string) $value);
|
||||
}
|
||||
|
||||
update_post_meta($post_id, $safe_key, $safe_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Form_Builder_Submissions();
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules\Forms;
|
||||
|
||||
use Elementor\Utils;
|
||||
use Elementor\Group_Control_Image_Size;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
/**
|
||||
* WPR_Subscribe_Mailchimp setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Subscribe_Mailchimp {
|
||||
|
||||
public function __construct() {
|
||||
// For form builder
|
||||
add_action( 'wp_ajax_wpr_form_builder_mailchimp', [$this, 'wpr_form_builder_mailchimp'] );
|
||||
add_action( 'wp_ajax_nopriv_wpr_form_builder_mailchimp', [$this, 'wpr_form_builder_mailchimp'] );
|
||||
}
|
||||
|
||||
/**
|
||||
** Mailchimp AJAX Subscribe
|
||||
*/
|
||||
public static function wpr_form_builder_mailchimp() {
|
||||
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
// API Key
|
||||
$api_key = get_option('wpr_mailchimp_api_key') ? get_option('wpr_mailchimp_api_key') : '';
|
||||
|
||||
// Validate API key format
|
||||
if (!preg_match('/^[0-9a-f]{32}-[a-z]{2}[0-9]{1,2}$/', $api_key)) {
|
||||
wp_send_json_error('Invalid API key format');
|
||||
return;
|
||||
}
|
||||
|
||||
$api_parts = explode('-', $api_key);
|
||||
if (count($api_parts) !== 2) {
|
||||
wp_send_json_error('Invalid API key format');
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate datacenter suffix
|
||||
$api_key_suffix = $api_parts[1];
|
||||
if (!preg_match('/^[a-z]{2}[0-9]{1,2}$/', $api_key_suffix)) {
|
||||
wp_send_json_error('Invalid API datacenter');
|
||||
return;
|
||||
}
|
||||
|
||||
// List ID with sanitization
|
||||
$list_id = isset($_POST['listId']) ? sanitize_text_field(wp_unslash($_POST['listId'])) : '';
|
||||
|
||||
// Get Available Fields (PHPCS - fields are sanitized later on input)
|
||||
$fields = isset($_POST['form_data']) ? $_POST['form_data'] : []; // phpcs:ignore
|
||||
|
||||
$group_ids = isset($fields['group_id']) ? array_map('sanitize_text_field', array_map('trim', explode(',', wp_unslash($fields['group_id'])))) : [];
|
||||
|
||||
// Merge Additional Fields
|
||||
$merge_fields = [
|
||||
'FNAME' => !empty( $fields['first_name_field'] ) ? sanitize_text_field($fields['first_name_field']) : '',
|
||||
'LNAME' => !empty( $fields['last_name_field'] ) ? sanitize_text_field($fields['last_name_field']) : '',
|
||||
'PHONE' => !empty ( $fields['phone_field'] ) ? sanitize_text_field($fields['phone_field']) : '',
|
||||
'BIRTHDAY' => !empty ( $fields['birthday_field'] ) ? sanitize_text_field($fields['birthday_field']) : '',
|
||||
];
|
||||
|
||||
$requiredKeys = ['address_field', 'country_field', 'city_field', 'state_field', 'zip_field'];
|
||||
|
||||
if ( !empty(array_intersect_key($fields, array_flip($requiredKeys))) ) {
|
||||
$merge_fields = array_merge($merge_fields, [
|
||||
'ADDRESS' => [
|
||||
'addr1' => !empty ( $fields['address_field'] ) ? sanitize_text_field($fields['address_field']) : 'none',
|
||||
'country' => !empty ( $fields['country_field'] ) ? sanitize_text_field($fields['country_field']) : 'none',
|
||||
'city' => !empty ( $fields['city_field'] ) ? sanitize_text_field($fields['city_field']) : 'none',
|
||||
'state' => !empty ( $fields['state_field'] ) ? sanitize_text_field($fields['state_field']) : 'none',
|
||||
'zip' =>!empty ( $fields['zip_field'] ) ? sanitize_text_field($fields['zip_field']) : 'none',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// API URL
|
||||
$api_url = 'https://'. $api_key_suffix .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members/'. md5(strtolower(sanitize_text_field($fields['email_field'])));
|
||||
|
||||
$api_body = [
|
||||
'email_address' => sanitize_text_field($fields[ 'email_field' ]),
|
||||
'status' => 'subscribed',
|
||||
'merge_fields' => $merge_fields
|
||||
];
|
||||
|
||||
if ( !empty($group_ids) ) {
|
||||
$api_body['interests'] = self::group_ids_to_interests_array($group_ids);
|
||||
}
|
||||
|
||||
// API Args
|
||||
$api_args = [
|
||||
'method' => 'PUT',
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => 'apikey '. $api_key,
|
||||
],
|
||||
'body' => json_encode($api_body),
|
||||
];
|
||||
|
||||
// Send Request
|
||||
$request = wp_remote_post( $api_url, $api_args );
|
||||
|
||||
if ( ! is_wp_error($request) ) {
|
||||
$request = json_decode( wp_remote_retrieve_body($request) );
|
||||
|
||||
// Set Status
|
||||
if ( ! empty($request) ) {
|
||||
if ($request->status == 'subscribed') {
|
||||
|
||||
wp_send_json_success(array(
|
||||
'action' => 'wpr_form_builder_mailchimp',
|
||||
'status' => 'success',
|
||||
'message' => 'Mailchimp subscription was successful',
|
||||
'request' => $request
|
||||
));
|
||||
|
||||
} else {
|
||||
wp_send_json_error([
|
||||
'action' => 'wpr_form_builder_mailchimp',
|
||||
'status' => 'error',
|
||||
'message' => 'Mailchimp subscription failed',
|
||||
'request' => $request
|
||||
]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
wp_send_json_error([
|
||||
'action' => 'wpr_form_builder_mailchimp',
|
||||
'status' => 'error',
|
||||
'message' => 'Mailchimp subscription failed',
|
||||
'request' => $request
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function group_ids_to_interests_array($group_ids) {
|
||||
$interests_array = [];
|
||||
|
||||
foreach ($group_ids as $group_id) {
|
||||
$interests_array[$group_id] = true;
|
||||
}
|
||||
|
||||
return $interests_array;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new WPR_Subscribe_Mailchimp();
|
||||
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules;
|
||||
|
||||
use Elementor\Utils;
|
||||
use Elementor\Group_Control_Image_Size;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Ajax_Search setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Ajax_Search {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_data_fetch' , [$this, 'data_fetch']);
|
||||
add_action('wp_ajax_nopriv_wpr_data_fetch',[$this, 'data_fetch']);
|
||||
}
|
||||
|
||||
public function data_fetch() {
|
||||
|
||||
$nonce = $_POST['nonce'];
|
||||
|
||||
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
|
||||
return; // Get out of here, the nonce is rotten!
|
||||
}
|
||||
|
||||
$all_post_types = [];
|
||||
|
||||
if ( sanitize_text_field($_POST['wpr_show_attachments']) == 'yes' ) {
|
||||
$all_post_types = ['attachment'];
|
||||
}
|
||||
|
||||
foreach( Utilities::get_custom_types_of( 'post', false ) as $key=>$value ) {
|
||||
array_push($all_post_types, $key);
|
||||
}
|
||||
|
||||
$tax_query = '';
|
||||
|
||||
if ( $_POST['wpr_category'] != false && $_POST['wpr_category'] != '' ) {
|
||||
$tax_query = array(
|
||||
array(
|
||||
'taxonomy' => $_POST['wpr_option_post_type'],
|
||||
'field' => 'term_id',
|
||||
'terms' => sanitize_text_field($_POST['wpr_category']),
|
||||
),
|
||||
);
|
||||
|
||||
// if ( isset($_POST['wpr_option_post_type']) && !empty($_POST['wpr_option_post_type']) ) {
|
||||
// $tax_query = array(
|
||||
// array(
|
||||
// 'taxonomy' => $_POST['wpr_option_post_type'],
|
||||
// 'field' => 'term_id',
|
||||
// 'terms' => sanitize_text_field($_POST['wpr_category']),
|
||||
// ),
|
||||
// );
|
||||
// } else {
|
||||
// $tax_query = array(
|
||||
// array(
|
||||
// 'taxonomy' => $_POST['wpr_query_type'] == 'product' ? $_POST['wpr_query_type'] . '_cat' : 'category',
|
||||
// 'field' => 'term_id',
|
||||
// 'terms' => sanitize_text_field($_POST['wpr_category']),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
} else if ( $_POST['wpr_category'] == 0 && $_POST['wpr_query_type'] != 'all' ) {
|
||||
if ( !empty($_POST['wpr_option_post_type']) ) {
|
||||
$tax_query = array(
|
||||
array(
|
||||
'taxonomy' => $_POST['wpr_option_post_type'],
|
||||
'field' => 'term_id',
|
||||
'terms' => sanitize_text_field($_POST['wpr_category']),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// Get the string from the POST data
|
||||
$taxonomy_type_string = $_POST['wpr_taxonomy_type'];
|
||||
|
||||
// Check if the string contains spaces
|
||||
if (strpos($taxonomy_type_string, ' ') !== false) {
|
||||
// Split the string into an array based on spaces
|
||||
$taxonomy_types = explode(' ', $taxonomy_type_string);
|
||||
|
||||
|
||||
$tax_query = [
|
||||
'relation' => 'OR'
|
||||
];
|
||||
|
||||
foreach( $taxonomy_types as $taxonomy_type ) {
|
||||
array_push($tax_query, [
|
||||
'taxonomy' => $taxonomy_type,
|
||||
'operator' => 'EXISTS'
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
// If there are no spaces, leave it as a single-item array
|
||||
$taxonomy_types = $taxonomy_type_string;
|
||||
|
||||
$tax_query = array(
|
||||
array(
|
||||
'taxonomy' => $_POST['wpr_taxonomy_type'],
|
||||
'operator' => 'EXISTS',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$can_view_protected_posts = current_user_can('read_private_posts');
|
||||
|
||||
if ( ( 'yes' !== sanitize_text_field($_POST['wpr_show_ps_pt'] ) ) || !$can_view_protected_posts ) {
|
||||
$the_query = new \WP_Query(
|
||||
[
|
||||
'posts_per_page' => sanitize_text_field($_POST['wpr_number_of_results']),
|
||||
's' => sanitize_text_field( $_POST['wpr_keyword'] ),
|
||||
'post_type' => $_POST['wpr_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ? $all_post_types : array( sanitize_text_field($_POST['wpr_query_type']) ),
|
||||
'offset' => sanitize_text_field($_POST['wpr_search_results_offset']),
|
||||
'meta_query' => 'yes' === sanitize_text_field($_POST['wpr_exclude_without_thumb']) ? [
|
||||
['key' => '_thumbnail_id']
|
||||
] : '',
|
||||
'tax_query' => $tax_query,
|
||||
'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish',
|
||||
'post_password' => ''
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$the_query = new \WP_Query(
|
||||
[
|
||||
'posts_per_page' => sanitize_text_field($_POST['wpr_number_of_results']),
|
||||
's' => sanitize_text_field( $_POST['wpr_keyword'] ),
|
||||
'post_type' => $_POST['wpr_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ? $all_post_types : array( sanitize_text_field($_POST['wpr_query_type']) ),
|
||||
'offset' => sanitize_text_field($_POST['wpr_search_results_offset']),
|
||||
'meta_query' => 'yes' === sanitize_text_field($_POST['wpr_exclude_without_thumb']) ? [
|
||||
['key' => '_thumbnail_id']
|
||||
] : '',
|
||||
'tax_query' => $tax_query,
|
||||
'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if( $the_query->have_posts() ) :
|
||||
$number_of_queried_posts = $the_query->found_posts;
|
||||
$post_count = 0;
|
||||
|
||||
while( $the_query->have_posts() ) : $the_query->the_post();
|
||||
|
||||
// if ( ( !has_post_thumbnail() && 'yes' === sanitize_text_field($_POST['wpr_exclude_without_thumb'])) ) :
|
||||
// continue;
|
||||
// endif;
|
||||
|
||||
ob_start();
|
||||
// the_post_thumbnail(sanitize_text_field($_POST['ajax_search_img_size']));
|
||||
the_post_thumbnail('medium');
|
||||
$post_thumb = ob_get_clean();
|
||||
?>
|
||||
|
||||
<li data-number-of-results = <?php echo $the_query->found_posts ?>>
|
||||
|
||||
<?php if ( !post_password_required() || $can_view_protected_posts ) : ?>
|
||||
|
||||
<?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_ajax_thumbnail']) ) :
|
||||
if ( has_post_thumbnail() ) :
|
||||
echo '<a class="wpr-ajax-img-wrap" target="'. esc_attr($_POST['wpr_ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'">'. $post_thumb .'</a>';
|
||||
// echo '<a class="wpr-ajax-img-wrap" target="'. sanitize_text_field($_POST['ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'">'. '<img src="'. Group_Control_Image_Size::get_attachment_image_src( get_post_thumbnail_id(), 'ajax_search_image', [$_POST['ajax_search_image_size']] ) .'"/>' .'</a>';
|
||||
else :
|
||||
echo '<a class="wpr-ajax-img-wrap" target="'. esc_attr($_POST['wpr_ajax_search_link_target']) .'" href='. esc_url( get_the_permalink() ) .'><img src='.Utils::get_placeholder_image_src().'></a>';
|
||||
endif ;
|
||||
endif ; ?>
|
||||
|
||||
<div class="wpr-ajax-search-content">
|
||||
<a target="<?php echo esc_attr($_POST['wpr_ajax_search_link_target']) ?>" class="wpr-ajax-title" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title();?></a>
|
||||
|
||||
<?php if ( sanitize_text_field($_POST['wpr_show_description']) == 'yes' ) : ?>
|
||||
<p class="wpr-ajax-desc"><a target="<?php echo esc_attr($_POST['wpr_ajax_search_link_target']) ?>" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo wp_trim_words(get_the_content(), sanitize_text_field($_POST['wpr_number_of_words'])); ?></a></p>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_product_price']) &&
|
||||
get_post_type() === 'product' &&
|
||||
class_exists('WooCommerce') ) :
|
||||
$product = wc_get_product(get_the_ID());
|
||||
if ($product) {
|
||||
$price_html = '<div class="wpr-search-product-price">'. $product->get_price_html() .'</div>';
|
||||
|
||||
echo $price_html;
|
||||
}
|
||||
endif; ?>
|
||||
|
||||
<?php if ( sanitize_text_field($_POST['wpr_show_view_result_btn']) ) : ?>
|
||||
<a target="<?php echo esc_attr($_POST['wpr_ajax_search_link_target']) ?>" class="wpr-view-result" href="<?php echo esc_url( the_permalink() ); ?>"><?php echo sanitize_text_field($_POST['wpr_view_result_text']) ?></a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<?php else: ?>
|
||||
|
||||
<?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_ajax_thumbnail']) ) :
|
||||
echo '<a class="wpr-ajax-img-wrap" target="'. esc_attr($_POST['wpr_ajax_search_link_target']) .'" href='. esc_url( get_the_permalink() ) .'><img src='.Utils::get_placeholder_image_src().'></a>';
|
||||
endif ; ?>
|
||||
|
||||
<div class="wpr-ajax-search-content">
|
||||
<a target="<?php echo esc_attr($_POST['wpr_ajax_search_link_target']) ?>" class="wpr-ajax-title" href="<?php echo esc_url( the_permalink() ); ?>"><?php the_title();?></a>
|
||||
</div>
|
||||
|
||||
<?php endif; ?>
|
||||
|
||||
</li>
|
||||
<?php
|
||||
$post_count++;
|
||||
endwhile;
|
||||
|
||||
wp_reset_postdata();
|
||||
|
||||
else :
|
||||
if (0 < sanitize_text_field($_POST['wpr_search_results_offset'])) {
|
||||
} else {
|
||||
echo '<p class="wpr-no-results">'. sanitize_text_field($_POST['wpr_no_results']) .'</p>';
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Ajax_Search();
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-recaptcha-handler.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-actions-status.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-send-email.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-send-webhook.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-file-upload.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-submissions-cpt.php';
|
||||
require WPR_ADDONS_PATH . 'classes/modules/forms/wpr-subscribe-mailchimp.php';
|
||||
File diff suppressed because it is too large
Load Diff
+459
@@ -0,0 +1,459 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules;
|
||||
|
||||
use Elementor\Utils;
|
||||
use Elementor\Group_Control_Image_Size;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Load_More_Instagram_Posts setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Load_More_Instagram_Posts {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_load_more_instagram_posts', [$this, 'wpr_load_more_instagram_posts_function']);
|
||||
add_action('wp_ajax_nopriv_wpr_load_more_instagram_posts', [$this, 'wpr_load_more_instagram_posts_function']);
|
||||
}
|
||||
|
||||
public function call_instagram_api($access_token, $settings) {
|
||||
$url = 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,thumbnail_url,permalink,username,caption,timestamp&access_token='. $access_token .'&limit='. ($settings['limit'] + $_POST['next_post_index']);
|
||||
$response = wp_remote_get($url);
|
||||
$body = json_decode($response['body']);
|
||||
if(!isset($body)) {
|
||||
return $response['body'];
|
||||
}
|
||||
return $body->data;
|
||||
}
|
||||
|
||||
// Get Animation Class
|
||||
public function get_animation_class( $data, $object ) {
|
||||
$class = '';
|
||||
|
||||
// Animation Class
|
||||
if ( 'none' !== $data[ $object .'_animation'] ) {
|
||||
$class .= ' wpr-'. $object .'-'. $data[ $object .'_animation'];
|
||||
$class .= ' wpr-anim-size-'. $data[ $object .'_animation_size'];
|
||||
$class .= ' wpr-anim-timing-'. $data[ $object .'_animation_timing'];
|
||||
|
||||
if ( 'yes' === $data[ $object .'_animation_tr'] ) {
|
||||
$class .= ' wpr-anim-transparency';
|
||||
}
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
// Render Post Title
|
||||
public function render_post_username( $settings, $class, $result ) {
|
||||
|
||||
$target = 'yes' == $_POST['wpr_load_more_settings']['open_in_new_tab'] ? '_blank' : '_self';
|
||||
|
||||
$tags_whitelist = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p'];
|
||||
$element_username_tag = Utilities::validate_html_tags_wl( $settings['element_username_tag'], 'h2', $tags_whitelist );
|
||||
|
||||
echo '<'. esc_attr($element_username_tag) .' class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<a href="'. $result->permalink .'" target="'. $target .'">';
|
||||
echo esc_html($result->username);
|
||||
echo '</a>';
|
||||
echo '</div>';
|
||||
echo '</'. esc_attr($element_username_tag) .'>';
|
||||
}
|
||||
|
||||
public function render_post_caption($settings, $class, $result) {
|
||||
|
||||
if ( !isset($result->caption) || '' === $result->caption ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<figcaption class="wpr-insta-feed-caption"><span>'. esc_html(wp_trim_words($result->caption, $settings['element_word_count'])) .'</span></figcaption>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_date($settings, $class, $result) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<span>';
|
||||
// Text: Before
|
||||
if ( 'before' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
// Icon: Before
|
||||
if ( 'before' === $settings['element_extra_icon_pos'] ) {
|
||||
echo '<i class="wpr-insta-feed-extra-icon-left '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
}
|
||||
|
||||
// Date
|
||||
if ( 'yes' === $settings['element_hide_year'] ) {
|
||||
echo date('F j', strtotime($result->timestamp));
|
||||
} else {
|
||||
echo date(get_option( 'date_format' ), strtotime($result->timestamp));
|
||||
}
|
||||
|
||||
// Icon: After
|
||||
if ( 'after' === $settings['element_extra_icon_pos'] ) {
|
||||
echo '<i class="wpr-insta-feed-extra-icon-right '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
}
|
||||
// Text: After
|
||||
if ( 'after' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
echo '</span>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_icon($settings, $class, $result) {
|
||||
|
||||
$target = 'yes' == $_POST['wpr_load_more_settings']['open_in_new_tab'] ? '_blank' : '_self';
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<a href='. $result->permalink .' target='. $target .'>';
|
||||
echo '<i class="fab fa-instagram"></i>';
|
||||
echo '</a>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_lightbox( $settings, $class, $result ) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
$lightbox_source = $result->media_url;
|
||||
|
||||
if ( 'VIDEO' === $result->media_type ) {
|
||||
$lightbox_source = $result->thumbnail_url;
|
||||
}
|
||||
|
||||
// Lightbox Button
|
||||
echo '<span data-src="'. esc_url( $lightbox_source ) .'">';
|
||||
|
||||
// Text: Before
|
||||
if ( 'before' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
|
||||
// Lightbox Icon
|
||||
echo '<i class="'. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
|
||||
// Text: After
|
||||
if ( 'after' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
|
||||
echo '</span>';
|
||||
|
||||
// Media Overlay
|
||||
if ( 'yes' === $settings['element_lightbox_overlay'] ) {
|
||||
echo '<div class="wpr-insta-feed-lightbox-overlay"></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_sharing_icons( $settings, $class, $result ) {
|
||||
$args = [
|
||||
'icons' => 'yes',
|
||||
'tooltip' => $settings['element_sharing_tooltip'],
|
||||
'url' => esc_url( $result->permalink ),
|
||||
'title' => esc_html( '' ),
|
||||
'text' => esc_html( isset($result->caption) ? $result->caption : '' ),
|
||||
'image' => esc_url( $result->media_url ),
|
||||
];
|
||||
|
||||
$hidden_class = '';
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
// Text: Before
|
||||
if ( 'before' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
|
||||
echo '<span class="wpr-post-sharing">';
|
||||
|
||||
if ( 'yes' === $settings['element_sharing_trigger'] ) {
|
||||
$hidden_class = ' wpr-sharing-hidden';
|
||||
$attributes = ' data-action="'. esc_attr( $settings['element_sharing_trigger_action'] ) .'"';
|
||||
$attributes .= ' data-direction="'. esc_attr( $settings['element_sharing_trigger_direction'] ) .'"';
|
||||
|
||||
echo '<a class="wpr-sharing-trigger wpr-sharing-icon"'. $attributes .'>';
|
||||
if ( 'yes' === $settings['element_sharing_tooltip'] ) {
|
||||
echo '<span class="wpr-sharing-tooltip wpr-tooltip">'. esc_html__( 'Share', 'wpr-addons' ) .'</span>';
|
||||
}
|
||||
|
||||
echo Utilities::get_wpr_icon( $settings['element_sharing_trigger_icon'], '' );
|
||||
echo '</a>';
|
||||
}
|
||||
|
||||
|
||||
echo '<span class="wpr-post-sharing-inner'. $hidden_class .'">';
|
||||
|
||||
for ( $i = 1; $i < 7; $i++ ) {
|
||||
$args['network'] = $settings['element_sharing_icon_'. $i];
|
||||
|
||||
echo Utilities::get_post_sharing_icon( $args );
|
||||
}
|
||||
|
||||
echo '</span>';
|
||||
|
||||
echo '</span>';
|
||||
|
||||
// Text: After
|
||||
if ( 'after' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-insta-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Render Post Element Separator
|
||||
public function render_post_element_separator( $settings, $class ) {
|
||||
echo '<div class="wpr-insta-feed-sep-style-1 '. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block"><span></span></div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Get Elements
|
||||
public function get_elements( $type, $settings, $class, $result ) {
|
||||
if ( 'pro-lk' == $type || 'pro-shr' == $type || 'pro-cf' == $type ) {
|
||||
$type = 'title';
|
||||
}
|
||||
|
||||
switch ( $type ) {
|
||||
|
||||
|
||||
case 'username':
|
||||
$this->render_post_username( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
case 'caption':
|
||||
$this->render_post_caption( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
case 'date':
|
||||
$this->render_post_date( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
case 'icon':
|
||||
$this->render_post_icon( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
// case 'comments':
|
||||
// $this->render_post_comments( $settings, $class );
|
||||
// break;
|
||||
|
||||
// case 'read-more':
|
||||
// $this->render_post_read_more( $settings, $class );
|
||||
// break;
|
||||
|
||||
// case 'likes':
|
||||
// $this->render_post_likes( $settings, $class, $post_id );
|
||||
// break;
|
||||
|
||||
case 'sharing':
|
||||
$this->render_post_sharing_icons( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
case 'lightbox':
|
||||
$this->render_post_lightbox( $settings, $class, $result );
|
||||
break;
|
||||
|
||||
case 'separator':
|
||||
$this->render_post_element_separator( $settings, $class );
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get Image Effect Class
|
||||
public function get_image_effect_class( $settings ) {
|
||||
$class = '';
|
||||
|
||||
if ( !defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code() ) {
|
||||
if ( 'pro-zi' == $settings['image_effects'] || 'pro-zo' == $settings['image_effects'] || 'pro-go' == $settings['image_effects'] || 'pro-bo' == $settings['image_effects'] ) {
|
||||
$settings['image_effects'] = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// Animation Class
|
||||
if ( 'none' !== $settings['image_effects'] ) {
|
||||
$class .= ' wpr-'. $settings['image_effects'];
|
||||
}
|
||||
|
||||
// Slide Effect
|
||||
if ( 'slide' !== $settings['image_effects'] ) {
|
||||
$class .= ' wpr-effect-size-'. $settings['image_effects_size'];
|
||||
} else {
|
||||
$class .= ' wpr-effect-dir-'. $settings['image_effects_direction'];
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
// Get Elements by Location
|
||||
public function get_elements_by_location( $location, $settings, $result ) {
|
||||
$locations = [];
|
||||
|
||||
foreach ( $settings['insta_feed_elements'] as $data ) {
|
||||
$place = $data['element_location'];
|
||||
$align_vr = $data['element_align_vr'];
|
||||
|
||||
if ( !defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code() ) {
|
||||
$align_vr = 'middle';
|
||||
}
|
||||
|
||||
if ( ! isset($locations[$place]) ) {
|
||||
$locations[$place] = [];
|
||||
}
|
||||
|
||||
if ( 'over' === $place ) {
|
||||
if ( ! isset($locations[$place][$align_vr]) ) {
|
||||
$locations[$place][$align_vr] = [];
|
||||
}
|
||||
|
||||
array_push( $locations[$place][$align_vr], $data );
|
||||
} else {
|
||||
array_push( $locations[$place], $data );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $locations[$location] ) ) {
|
||||
|
||||
if ( 'over' === $location ) {
|
||||
foreach ( $locations[$location] as $align => $thiss ) {
|
||||
|
||||
if ( 'middle' === $align ) {
|
||||
echo '<div class="wpr-cv-container"><div class="wpr-cv-outer"><div class="wpr-cv-inner">';
|
||||
}
|
||||
|
||||
echo '<div class="wpr-insta-feed-media-hover-'. esc_attr($align) .' elementor-clearfix">';
|
||||
foreach ( $thiss as $data ) {
|
||||
|
||||
// Get Class
|
||||
$class = 'wpr-insta-feed-item-'. $data['element_select'];
|
||||
$class .= ' elementor-repeater-item-'. $data['_id'];
|
||||
$class .= ' wpr-insta-feed-item-display-'. $data['element_display'];
|
||||
$class .= ' wpr-insta-feed-item-align-'. $data['element_align_hr'];
|
||||
$class .= $this->get_animation_class( $data, 'element' );
|
||||
|
||||
// Element
|
||||
$this->get_elements( $data['element_select'], $data, $class, $result );
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
if ( 'middle' === $align ) {
|
||||
echo '</div></div></div>';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$count_elements = 0;
|
||||
$caption_not_empty = true;
|
||||
foreach ( $locations[$location] as $data ) {
|
||||
$count_elements++;
|
||||
if ( 'caption' === $data['element_select'] ) {
|
||||
$caption_not_empty = isset($result->caption);
|
||||
}
|
||||
}
|
||||
|
||||
if ( $count_elements == 1 && !$caption_not_empty ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<div class="wpr-insta-feed-item-'. esc_attr($location) .'-content elementor-clearfix">';
|
||||
foreach ( $locations[$location] as $data ) {
|
||||
|
||||
// Get Class
|
||||
$class = 'wpr-insta-feed-item-'. $data['element_select'];
|
||||
$class .= ' elementor-repeater-item-'. $data['_id'];
|
||||
$class .= ' wpr-insta-feed-item-display-'. $data['element_display'];
|
||||
$class .= ' wpr-insta-feed-item-align-'. $data['element_align_hr'];
|
||||
|
||||
// Element
|
||||
$this->get_elements( $data['element_select'], $data, $class, $result );
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Render Media Overlay
|
||||
public function render_media_overlay( $settings, $result ) {
|
||||
|
||||
$target = 'yes' == $_POST['wpr_load_more_settings']['open_in_new_tab'] ? '_blank' : '_self';
|
||||
|
||||
echo '<div class="wpr-insta-feed-media-hover-bg '. esc_attr($this->get_animation_class( $settings, 'overlay' )) .'" data-url="'. $result->permalink .'" data-target="'. $target .'">';
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function wpr_load_more_instagram_posts_function() {
|
||||
$settings = $_POST['wpr_load_more_settings'];
|
||||
|
||||
if ( get_transient('wpr_instagram_access_token'. $_POST['wpr_insta_feed_widget_id']) ) {
|
||||
$instagram_token = get_transient('wpr_instagram_access_token'. $_POST['wpr_insta_feed_widget_id']);
|
||||
} else {
|
||||
$instagram_token = $settings['instagram_access_token'];
|
||||
}
|
||||
|
||||
foreach($this->call_instagram_api($instagram_token, $_POST['wpr_load_more_settings']) as $key=>$result) : ?>
|
||||
<?php
|
||||
if ( $key >= 6 && (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ) {
|
||||
break;
|
||||
}
|
||||
if ($key < $_POST['next_post_index']) :
|
||||
continue;
|
||||
endif;
|
||||
?>
|
||||
|
||||
<div class="wpr-insta-feed-content-wrap wpr-insta-col-12">
|
||||
<figure>
|
||||
<?php
|
||||
// Content: Below Media
|
||||
echo $this->get_elements_by_location( 'above', $_POST['wpr_load_more_settings'], $result );
|
||||
?>
|
||||
<div class="wpr-insta-feed-media-wrap <?php echo esc_attr($this->get_image_effect_class( $_POST['wpr_load_more_settings'] )) ?>" data-overlay-link="<?php echo esc_attr( $_POST['wpr_load_more_settings']['overlay_post_link'] ) ?>">
|
||||
<?php if ( 'CAROUSEL_ALBUM' == $result->media_type || 'IMAGE' == $result->media_type ) : ?>
|
||||
<div class="wpr-insta-feed-image-wrap" data-src=<?php echo $result->media_url ?>>
|
||||
<img src=<?php echo $result->media_url ?> alt="">
|
||||
</div>
|
||||
<?php else : ?>
|
||||
<div class="wpr-insta-feed-image-wrap" data-src=<?php echo $result->thumbnail_url ?>>
|
||||
<img class="wpr-insta-feed-thumb" src=<?php echo $result->thumbnail_url ?> alt="">
|
||||
</div>
|
||||
<?php endif ; ?>
|
||||
<div class="wpr-insta-feed-media-hover wpr-animation-wrap">
|
||||
<?php
|
||||
// Media Overlay
|
||||
$this->render_media_overlay( $_POST['wpr_load_more_settings'], $result );
|
||||
|
||||
// Content: Over Media
|
||||
$this->get_elements_by_location( 'over', $_POST['wpr_load_more_settings'], $result );
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
// Content: Below Media
|
||||
echo $this->get_elements_by_location( 'below', $_POST['wpr_load_more_settings'], $result );
|
||||
?>
|
||||
</figure>
|
||||
</div>
|
||||
<?php endforeach;
|
||||
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Load_More_Instagram_Posts();
|
||||
@@ -0,0 +1,461 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules;
|
||||
|
||||
use Elementor\Utils;
|
||||
use Elementor\Group_Control_Image_Size;
|
||||
use WprAddons\Classes\Utilities;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Load_More_Tweets setup
|
||||
*
|
||||
* @since 3.4.6
|
||||
*/
|
||||
|
||||
class WPR_Load_More_Tweets {
|
||||
|
||||
public function __construct() {
|
||||
add_action('wp_ajax_wpr_load_more_tweets', [$this, 'wpr_load_more_tweets_function']);
|
||||
add_action('wp_ajax_nopriv_wpr_load_more_tweets', [$this, 'wpr_load_more_tweets_function']);
|
||||
}
|
||||
|
||||
// Render User Name
|
||||
public function render_post_username( $settings, $class, $item ) {
|
||||
|
||||
$tags_whitelist = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p'];
|
||||
$element_username_tag = Utilities::validate_html_tags_wl( $settings['element_username_tag'], 'h2', $tags_whitelist );
|
||||
|
||||
echo '<'. esc_attr($element_username_tag) .' class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<a>'. $item['user']['name'] .'</a>';
|
||||
echo '</div>';
|
||||
echo '</'. esc_attr($element_username_tag) .'>';
|
||||
}
|
||||
|
||||
// Render User Account
|
||||
public function render_post_user_profile_name( $settings, $class, $item ) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<small>';
|
||||
echo '<a href="https://twitter.com/' . $item['user']['screen_name'] .'">@'. $item['user']['screen_name'] .'</a>';
|
||||
echo '</small>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Render User Account
|
||||
public function render_post_user_profile_picture( $settings, $class, $item ) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
?>
|
||||
<figure>
|
||||
<img src="<?php echo $item['user']['profile_image_url'] ?>" alt="Image">
|
||||
</figure>
|
||||
<?php
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_caption($settings, $class, $item) {
|
||||
|
||||
if ( !isset($item['full_text']) || '' === $item['full_text'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<p>';
|
||||
$string = preg_replace("~[[:alpha:]]+://[^<p>[:space:]]+[[:alnum:]/]~", "<a href=\"\\0\">\\0</a>", $item['full_text']);
|
||||
$new_string = preg_replace('/\#([a-z0-9]+)/i', '<a href="https://twitter.com/hashtag/$1?src=hashtag_click">#$1</a>', $string);
|
||||
echo preg_replace('/\@([a-z0-9]+)/i', '<a href="https://twitter.com/$1">@$1</a>' ,$new_string);
|
||||
echo '</p>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_date($settings, $class, $item) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
echo '<span>';
|
||||
|
||||
// Text: Before
|
||||
// if ( 'before' === $settings['element_extra_text_pos'] ) {
|
||||
// echo '<span class="wpr-twitter-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
// }
|
||||
// Icon: Before
|
||||
// if ( 'before' === $settings['element_extra_icon_pos'] ) {
|
||||
// echo '<i class="wpr-twitter-feed-extra-icon-left '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
// }
|
||||
|
||||
// echo wp_date(get_option( 'date_format' ), strtotime($item['created_at']))
|
||||
echo human_time_diff(strtotime($item['created_at'])) .' '. esc_html__('ago', 'wpr-addons');
|
||||
|
||||
// Icon: After
|
||||
// if ( 'after' === $settings['element_extra_icon_pos'] ) {
|
||||
// echo '<i class="wpr-twitter-feed-extra-icon-right '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
// }
|
||||
// Text: After
|
||||
// if ( 'after' === $settings['element_extra_text_pos'] ) {
|
||||
// echo '<span class="wpr-twitter-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
// }
|
||||
|
||||
echo '</span>';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_lightbox( $settings, $class, $item ) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
$lightbox_source = $item[''];
|
||||
|
||||
// Lightbox Button
|
||||
echo '<span data-src="'. esc_url( $lightbox_source ) .'">';
|
||||
|
||||
// Text: Before
|
||||
if ( 'before' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-twitter-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
|
||||
// Lightbox Icon
|
||||
echo '<i class="'. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>';
|
||||
|
||||
// Text: After
|
||||
if ( 'after' === $settings['element_extra_text_pos'] ) {
|
||||
echo '<span class="wpr-twitter-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>';
|
||||
}
|
||||
|
||||
echo '</span>';
|
||||
|
||||
// Media Overlay
|
||||
if ( 'yes' === $settings['element_lightbox_overlay'] ) {
|
||||
echo '<div class="wpr-twitter-feed-lightbox-overlay"></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_likes($settings, $class, $item) {
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
|
||||
?>
|
||||
<a href="https://twitter.com/intent/like?tweet_id=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Likes">
|
||||
<span class=""><i class="fas fa-heart"></i></span>
|
||||
<span class="wpr-tweet-likes">
|
||||
<?php echo $item['favorite_count'] ?>
|
||||
</span>
|
||||
</a>
|
||||
<?php
|
||||
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_comment($settings, $class, $item) {
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
|
||||
?>
|
||||
<a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Comments">
|
||||
<span class=""><i class="fas fa-comment"></i></span>
|
||||
</a>
|
||||
<?php
|
||||
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_retweets($settings, $class, $item) {
|
||||
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
|
||||
?>
|
||||
<a href="https://twitter.com/intent/retweet?tweet_id=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Retweets">
|
||||
<span class=""><i class="fa fa-retweet"></i></span>
|
||||
<span class="wpr-retweets">
|
||||
<?php echo $item['retweet_count'] ?>
|
||||
</span>
|
||||
</a>
|
||||
<?php
|
||||
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Render Post Element Separator
|
||||
public function render_post_element_separator( $settings, $class ) {
|
||||
echo '<div class="wpr-twitter-feed-sep-style-1 '. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block"><span></span></div>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
public function render_post_media($settings, $class, $item) {
|
||||
|
||||
if ( isset($item['extended_entities']) && null !== $item['extended_entities'] ) {
|
||||
if ( $item['extended_entities']['media'] ) {
|
||||
$media = $item['extended_entities']['media'];
|
||||
} else if ( isset( $item['retweeted_status']['entities']['media'] ) ) {
|
||||
$media = $item['retweeted_status']['entities']['media'];
|
||||
} else if ( isset( $item['quoted_status']['entities']['media'] ) ) {
|
||||
$media = $item['quoted_status']['entities']['media'];
|
||||
} else {
|
||||
$media = [];
|
||||
}
|
||||
}
|
||||
|
||||
if ( !empty($media) ) {
|
||||
echo '<div class="'. esc_attr($class) .'">';
|
||||
echo '<div class="inner-block">';
|
||||
// && $media[0]['type'] == 'photo'
|
||||
echo (isset( $media[0] )) ? '<img class="wpr-twit-image" src="' . $media[0]['media_url_https'] . '">' : '';
|
||||
echo '</div>';
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// Get Elements
|
||||
public function get_elements( $type, $settings, $class, $item ) {
|
||||
if ( 'pro-lk' == $type || 'pro-shr' == $type || 'pro-cf' == $type ) {
|
||||
$type = 'title';
|
||||
}
|
||||
|
||||
switch ( $type ) {
|
||||
|
||||
|
||||
case 'username':
|
||||
$this->render_post_username( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'profile-name':
|
||||
$this->render_post_user_profile_name( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'twit':
|
||||
$this->render_post_caption( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'date':
|
||||
$this->render_post_date( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'likes':
|
||||
$this->render_post_likes( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'comment':
|
||||
$this->render_post_comment( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'retweets':
|
||||
$this->render_post_retweets( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'media':
|
||||
$this->render_post_media( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
case 'profile-picture':
|
||||
$this->render_post_user_profile_picture( $settings, $class, $item );
|
||||
break;
|
||||
|
||||
// case 'read-more':
|
||||
// $this->render_post_account_name( $settings, $class );
|
||||
// break;
|
||||
|
||||
// case 'lightbox':
|
||||
// $this->render_post_lightbox( $settings, $class, $item );
|
||||
// break;
|
||||
|
||||
case 'separator':
|
||||
$this->render_post_element_separator( $settings, $class );
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Get Elements by Location
|
||||
public function get_elements_by_location( $location, $settings, $item ) {
|
||||
$locations = [];
|
||||
|
||||
foreach ( $settings['twitter_feed_elements'] as $data ) {
|
||||
$place = $data['element_location'];
|
||||
$align_vr = $data['element_align_vr'];
|
||||
|
||||
if ( !defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code() ) {
|
||||
$align_vr = 'middle';
|
||||
}
|
||||
|
||||
if ( ! isset($locations[$place]) ) {
|
||||
$locations[$place] = [];
|
||||
}
|
||||
|
||||
if ( 'over' === $place ) {
|
||||
if ( ! isset($locations[$place][$align_vr]) ) {
|
||||
$locations[$place][$align_vr] = [];
|
||||
}
|
||||
|
||||
array_push( $locations[$place][$align_vr], $data );
|
||||
} else {
|
||||
array_push( $locations[$place], $data );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $locations[$location] ) ) {
|
||||
|
||||
if ( 'over' === $location ) {
|
||||
foreach ( $locations[$location] as $align => $thiss ) {
|
||||
|
||||
if ( 'middle' === $align ) {
|
||||
echo '<div class="wpr-cv-container"><div class="wpr-cv-outer"><div class="wpr-cv-inner">';
|
||||
}
|
||||
|
||||
echo '<div class="wpr-twitter-feed-media-hover-'. esc_attr($align) .' elementor-clearfix">';
|
||||
foreach ( $thiss as $data ) {
|
||||
|
||||
// Get Class
|
||||
$class = 'wpr-twitter-feed-item-'. $data['element_select'];
|
||||
$class .= ' elementor-repeater-item-'. $data['_id'];
|
||||
$class .= ' wpr-twitter-feed-item-display-'. $data['element_display'];
|
||||
$class .= ' wpr-twitter-feed-item-align-'. $data['element_align_hr'];
|
||||
$class .= $this->get_animation_class( $data, 'element' );
|
||||
|
||||
// Element
|
||||
$this->get_elements( $data['element_select'], $data, $class, $item );
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
if ( 'middle' === $align ) {
|
||||
echo '</div></div></div>';
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo '<div class="wpr-twitter-feed-item-'. esc_attr($location) .'-content elementor-clearfix">';
|
||||
foreach ( $locations[$location] as $data ) {
|
||||
|
||||
// Get Class
|
||||
$class = 'wpr-twitter-feed-item-'. $data['element_select'];
|
||||
$class .= ' elementor-repeater-item-'. $data['_id'];
|
||||
$class .= ' wpr-twitter-feed-item-display-'. $data['element_display'];
|
||||
$class .= ' wpr-twitter-feed-item-align-'. $data['element_align_hr'];
|
||||
|
||||
// Element
|
||||
$this->get_elements( $data['element_select'], $data, $class, $item );
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function wpr_load_more_tweets_function() {
|
||||
|
||||
$settings = $_POST['wpr_load_more_settings'];
|
||||
|
||||
$credentials = base64_encode($settings['twitter_feed_consumer_key'] . ':' . $settings['twitter_feed_consumer_secret']);
|
||||
|
||||
add_filter('https_ssl_verify', '__return_false');
|
||||
|
||||
$response = wp_remote_post('https://api.twitter.com/oauth2/token', [
|
||||
'method' => 'POST',
|
||||
'httpversion' => '1.1',
|
||||
'blocking' => true,
|
||||
'headers' => [
|
||||
'Authorization' => 'Basic ' . $credentials,
|
||||
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
|
||||
],
|
||||
'body' => ['grant_type' => 'client_credentials'],
|
||||
]);
|
||||
|
||||
$body = json_decode(wp_remote_retrieve_body($response));
|
||||
|
||||
if ($body) {
|
||||
$token = $body->access_token;
|
||||
}
|
||||
|
||||
add_filter('https_ssl_verify', '__return_false');
|
||||
|
||||
$response = [];
|
||||
$items_array = [];
|
||||
|
||||
foreach ($settings['twitter_accounts'] as $key=>$value) {
|
||||
|
||||
$response[$key] = wp_remote_get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $value['twitter_feed_account_name'] . '&count='. ($settings['number_of_posts'] + $_POST['next_post_index']) .'&tweet_mode=extended', [
|
||||
'httpversion' => '1.1',
|
||||
'blocking' => true,
|
||||
'headers' => [
|
||||
'Authorization' => "Bearer $token",
|
||||
],
|
||||
]);
|
||||
|
||||
if ( is_wp_error( $response[$key] ) ) {
|
||||
return $response[$key];
|
||||
}
|
||||
|
||||
if ( ! empty( $response[$key]['response'] ) && $response[$key]['response']['code'] == 200 ) {
|
||||
$items_array[] = json_decode( wp_remote_retrieve_body( $response[$key] ), true );
|
||||
}
|
||||
}
|
||||
foreach ( $items_array as $key=>$items ) :
|
||||
|
||||
if ($settings['twitter_feed_hashtag_name']) {
|
||||
$hashtag_names = explode(',', str_replace(' ', '', $settings['twitter_feed_hashtag_name']));
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
$match = false;
|
||||
|
||||
if ($item['entities']['hashtags']) {
|
||||
foreach ($item['entities']['hashtags'] as $tag) {
|
||||
if (in_array($tag['text'], $hashtag_names)) {
|
||||
$match = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($match == false) {
|
||||
unset($items[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $items as $key=>$item) :
|
||||
|
||||
if ( $key >= 6 && (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($key < $_POST['next_post_index']) :
|
||||
continue;
|
||||
endif;
|
||||
|
||||
$banner_placeholder = WPR_ADDONS_ASSETS_URL . 'img/placeholder.png';
|
||||
$banner = $item['user']['profile_banner_url'] ? $item['user']['profile_banner_url'] : $banner_placeholder;
|
||||
?>
|
||||
<div class="wpr-tweet">
|
||||
<img src="<?php echo $banner ?>" alt="">
|
||||
<article class="media">
|
||||
|
||||
<?php
|
||||
// Content: Above Media
|
||||
echo $this->get_elements_by_location( 'above', $settings, $item );
|
||||
?>
|
||||
|
||||
</article>
|
||||
</div>
|
||||
<?php
|
||||
|
||||
endforeach;
|
||||
endforeach;
|
||||
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Load_More_Tweets();
|
||||
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
namespace WprAddons\Classes\Modules;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* WPR_Post_Likes setup
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class WPR_Post_Likes {
|
||||
|
||||
/**
|
||||
** Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'wp_ajax_nopriv_wpr_likes_init', [ $this, 'wpr_likes_init' ] );
|
||||
add_action( 'wp_ajax_wpr_likes_init', [ $this, 'wpr_likes_init' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
** Likes Init
|
||||
*/
|
||||
public function wpr_likes_init() {
|
||||
// Security
|
||||
$nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash($_REQUEST['nonce']) ) : 0;
|
||||
|
||||
if ( ! wp_verify_nonce( $nonce, 'wpr-post-likes-nonce' ) ) {
|
||||
exit( esc_html__( 'Not permitted', 'wpr-addons' ) );
|
||||
}
|
||||
|
||||
// Test if javascript is disabled
|
||||
$js_disabled = ( isset( $_REQUEST['disabled'] ) && $_REQUEST['disabled'] == true ) ? true : false;
|
||||
|
||||
// Base variables
|
||||
$post_id = ( isset( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) ? absint($_REQUEST['post_id']) : '';
|
||||
|
||||
$post_users = NULL;
|
||||
$like_count = 0;
|
||||
|
||||
// Init
|
||||
if ( $post_id != '' ) {
|
||||
// Likes Count
|
||||
$count = get_post_meta( $post_id, '_post_like_count', true );
|
||||
$count = ( isset( $count ) && is_numeric( $count ) ) ? $count : 0;
|
||||
|
||||
// Like the Post
|
||||
if ( ! $this->already_liked( $post_id ) ) {
|
||||
// Logged-in User
|
||||
if ( is_user_logged_in() ) {
|
||||
$user_id = get_current_user_id();
|
||||
$post_users = $this->get_user_likes( $user_id, $post_id );
|
||||
|
||||
// Get Like Count
|
||||
$user_like_count = get_user_option( '_user_like_count', $user_id );
|
||||
$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
|
||||
|
||||
// Update Like Count
|
||||
update_user_option( $user_id, '_user_like_count', ++$user_like_count );
|
||||
|
||||
// Update Post
|
||||
if ( $post_users ) {
|
||||
update_post_meta( $post_id, '_user_liked', $post_users );
|
||||
}
|
||||
|
||||
// Anonymous User
|
||||
} else {
|
||||
$post_users = $this->get_IP_likes( $this->get_IP(), $post_id );
|
||||
|
||||
// Update Post
|
||||
if ( $post_users ) {
|
||||
update_post_meta( $post_id, '_user_IP', $post_users );
|
||||
}
|
||||
}
|
||||
|
||||
// Send to JS
|
||||
$like_count = ++$count;
|
||||
$response['status'] = 'liked';
|
||||
|
||||
// Unlike the Post
|
||||
} else {
|
||||
// Logged-in User
|
||||
if ( is_user_logged_in() ) {
|
||||
$user_id = get_current_user_id();
|
||||
$post_users = $this->get_user_likes( $user_id, $post_id );
|
||||
|
||||
// Get Like Count
|
||||
$user_like_count = get_user_option( '_user_like_count', $user_id );
|
||||
$user_like_count = ( isset( $user_like_count ) && is_numeric( $user_like_count ) ) ? $user_like_count : 0;
|
||||
|
||||
// Update Like Count
|
||||
if ( $user_like_count > 0 ) {
|
||||
update_user_option( $user_id, '_user_like_count', --$user_like_count );
|
||||
}
|
||||
|
||||
// Update Post
|
||||
if ( $post_users ) {
|
||||
$uid_key = array_search( $user_id, $post_users );
|
||||
unset( $post_users[$uid_key] );
|
||||
update_post_meta( $post_id, '_user_liked', $post_users );
|
||||
}
|
||||
|
||||
// Anonymous User
|
||||
} else {
|
||||
$post_users = $this->get_IP_likes( $this->get_IP(), $post_id );
|
||||
|
||||
// Update Post
|
||||
if ( $post_users ) {
|
||||
|
||||
unset( $post_users[ array_search( $this->get_IP(), $post_users ) ] );
|
||||
update_post_meta( $post_id, '_user_IP', $post_users );
|
||||
}
|
||||
}
|
||||
|
||||
// Send to JS
|
||||
$like_count = ( $count > 0 ) ? --$count : 0;
|
||||
$response['status'] = 'unliked';
|
||||
}
|
||||
|
||||
// Update Post
|
||||
update_post_meta( $post_id, '_post_like_count', $like_count );
|
||||
update_post_meta( $post_id, '_post_like_modified', date( 'Y-m-d H:i:s' ) );
|
||||
|
||||
// Send to JS
|
||||
$response['count'] = $this->get_like_count( $like_count );
|
||||
|
||||
// JavaScript Disabled
|
||||
if ( $js_disabled == true ) {
|
||||
wp_redirect( get_permalink( $post_id ) );
|
||||
exit();
|
||||
} else {
|
||||
wp_send_json( $response );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
** Get Button
|
||||
*/
|
||||
public function get_button( $post_id, $settings ) {
|
||||
$nonce = wp_create_nonce( 'wpr-post-likes-nonce' ); // Security
|
||||
$like_count = get_post_meta( $post_id, '_post_like_count', true );
|
||||
$like_count = ( isset( $like_count ) && is_numeric( $like_count ) ) ? $like_count : 0;
|
||||
$default_text_class = '';
|
||||
$button_text = $settings['element_like_text'];
|
||||
|
||||
// Already Liked
|
||||
if ( $this->already_liked( $post_id ) ) {
|
||||
$title = esc_html__( 'Like', 'wpr-addons' );
|
||||
$liked_class = esc_attr( ' wpr-already-liked' );
|
||||
$icon_class = str_replace( 'far', 'fas', $settings['element_like_icon'] );
|
||||
} else {
|
||||
$title = esc_html__( 'Unlike', 'wpr-addons' );
|
||||
$liked_class = '';
|
||||
$icon_class = $settings['element_like_icon'];
|
||||
}
|
||||
|
||||
// Default Text
|
||||
if ( '' === $settings['element_like_text'] ) {
|
||||
$default_text_class = ' wpr-likes-no-default';
|
||||
}
|
||||
|
||||
// Zero Likes Class
|
||||
if ( 0 == $like_count ) {
|
||||
$default_text_class .= ' wpr-likes-zero';
|
||||
}
|
||||
|
||||
// Button Attributes
|
||||
$attributes = 'href="'. esc_url(admin_url( 'admin-ajax.php?action=wpr_likes_init&post_id='. $post_id .'&nonce='. $nonce )) .'"';
|
||||
$attributes .= ' class="wpr-post-like-button'. esc_attr($liked_class . $default_text_class) .'"';
|
||||
$attributes .= ' title="'. esc_attr($title) .'"';
|
||||
$attributes .= ' data-nonce="'. esc_attr($nonce) .'"';
|
||||
$attributes .= ' data-post-id="'. esc_attr($post_id) .'"';
|
||||
$attributes .= ' data-ajax="'. esc_url(admin_url( 'admin-ajax.php' )) .'"';
|
||||
$attributes .= ' data-icon="'. esc_attr($icon_class) .'"';
|
||||
$attributes .= ' data-text="'. esc_attr($button_text) .'"';
|
||||
|
||||
// Output
|
||||
$output = '<a '. $attributes .'>';
|
||||
$output .= '<i class="'. esc_attr($icon_class) .'"></i>';
|
||||
$output .= $this->get_like_count( $like_count, $button_text );
|
||||
$output .= '</a>';
|
||||
|
||||
return $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
** Utility: Already Liked
|
||||
*/
|
||||
public function already_liked( $post_id ) {
|
||||
$post_users = NULL;
|
||||
$user_id = NULL;
|
||||
|
||||
// Logged-in User
|
||||
if ( is_user_logged_in() ) {
|
||||
$user_id = get_current_user_id();
|
||||
$post_meta_users = get_post_meta( $post_id, '_user_liked' );
|
||||
|
||||
if ( count( $post_meta_users ) != 0 ) {
|
||||
$post_users = $post_meta_users[0];
|
||||
}
|
||||
|
||||
// Anonymous User
|
||||
} else {
|
||||
$user_id = $this->get_IP();
|
||||
$post_meta_users = get_post_meta( $post_id, '_user_IP' );
|
||||
|
||||
if ( count( $post_meta_users ) != 0 ) {
|
||||
$post_users = $post_meta_users[0];
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_array( $post_users ) && in_array( $user_id, $post_users ) ) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
** Utility: Get User Likes
|
||||
*/
|
||||
public function get_user_likes( $user_id, $post_id ) {
|
||||
$post_users = '';
|
||||
$post_meta_users = get_post_meta( $post_id, '_user_liked' );
|
||||
|
||||
if ( count( $post_meta_users ) != 0 ) {
|
||||
$post_users = $post_meta_users[0];
|
||||
}
|
||||
|
||||
if ( !is_array( $post_users ) ) {
|
||||
$post_users = array();
|
||||
}
|
||||
|
||||
if ( !in_array( $user_id, $post_users ) ) {
|
||||
$post_users['user-'. $user_id] = $user_id;
|
||||
}
|
||||
|
||||
return $post_users;
|
||||
}
|
||||
|
||||
/**
|
||||
** Utility: Get IP Likes
|
||||
*/
|
||||
public function get_IP_likes( $user_ip, $post_id ) {
|
||||
$post_users = '';
|
||||
$post_meta_users = get_post_meta( $post_id, '_user_IP' );
|
||||
|
||||
// Retrieve post information
|
||||
if ( count( $post_meta_users ) != 0 ) {
|
||||
$post_users = $post_meta_users[0];
|
||||
}
|
||||
|
||||
if ( !is_array( $post_users ) ) {
|
||||
$post_users = array();
|
||||
}
|
||||
|
||||
if ( !in_array( $user_ip, $post_users ) ) {
|
||||
$post_users['ip-'. $user_ip] = $user_ip;
|
||||
}
|
||||
|
||||
return $post_users;
|
||||
}
|
||||
|
||||
/**
|
||||
** Utility: Get IP
|
||||
*/
|
||||
public function get_IP() {
|
||||
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) && ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
|
||||
$ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP']));
|
||||
} else {
|
||||
$ip = ( isset( $_SERVER['REMOTE_ADDR'] ) ) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '0.0.0.0';
|
||||
}
|
||||
|
||||
$ip = filter_var( $ip, FILTER_VALIDATE_IP );
|
||||
$ip = ( $ip === false ) ? '0.0.0.0' : $ip;
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
** Utility: Format Likes Number
|
||||
*/
|
||||
public function get_format_count( $number ) {
|
||||
$precision = 2;
|
||||
|
||||
if ( $number >= 1000 && $number < 1000000 ) {
|
||||
$formatted = number_format( $number/1000, $precision ).'K';
|
||||
} elseif ( $number >= 1000000 && $number < 1000000000 ) {
|
||||
$formatted = number_format( $number/1000000, $precision ).'M';
|
||||
} elseif ( $number >= 1000000000 ) {
|
||||
$formatted = number_format( $number/1000000000, $precision ).'B';
|
||||
} else {
|
||||
$formatted = $number; // Number is less than 1000
|
||||
}
|
||||
|
||||
$formatted = str_replace( '.00', '', $formatted );
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
** Utility: Get Likes Count
|
||||
*/
|
||||
public function get_like_count( $like_count, $like_text = '' ) {
|
||||
if ( is_numeric( $like_count ) && $like_count > 0 ) {
|
||||
$number = $this->get_format_count( $like_count );
|
||||
} else {
|
||||
$number = $like_text;
|
||||
}
|
||||
|
||||
return '<span class="wpr-post-like-count">'. esc_html($number) .'</span>';
|
||||
}
|
||||
}
|
||||
|
||||
new WPR_Post_Likes();
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user