first commit
This commit is contained in:
155
vendor/wpcfto/helpers/file_upload.php
vendored
Normal file
155
vendor/wpcfto/helpers/file_upload.php
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
new STM_WPCFTO_FILE_UPLOAD();
|
||||
|
||||
class STM_WPCFTO_FILE_UPLOAD {
|
||||
|
||||
public function __construct() {
|
||||
add_action( 'wp_ajax_wpcfto_upload_file', array( $this, 'upload_file' ) );
|
||||
}
|
||||
|
||||
public function upload_file() {
|
||||
check_ajax_referer( 'wpcfto_upload_file', 'nonce' );
|
||||
|
||||
if ( ! empty( $_FILES['file'] ) && ! empty( $_FILES['file']['name'] ) ) {
|
||||
$filename = sanitize_text_field( $_FILES['file']['name'] );
|
||||
do_action( 'stm_lms_nuxy_repeater_upload_file', $filename );
|
||||
}
|
||||
|
||||
$this->create_folder();
|
||||
|
||||
$r = array(
|
||||
'error' => apply_filters( 'wpcfto_file_error_label', __( 'Error occurred, please try again', 'nuxy' ) ),
|
||||
'path' => '',
|
||||
'url' => '',
|
||||
);
|
||||
|
||||
if ( empty( $_POST['field'] ) ) {
|
||||
wp_send_json( $r );
|
||||
}
|
||||
|
||||
$field = sanitize_text_field( $_POST['field'] );
|
||||
|
||||
/*is_repeater?*/
|
||||
if ( ! empty( $_POST['field_native_name'] ) && ! empty( $_POST['field_native_name_inner'] ) ) {
|
||||
$field = sanitize_text_field( $_POST['field_native_name'] );
|
||||
$field_inner = sanitize_text_field( $_POST['field_native_name_inner'] );
|
||||
}
|
||||
|
||||
$field_data = $this->get_field_data( $field );
|
||||
|
||||
if ( ! empty( $field_inner ) && ! empty( $field_data ) && ! empty( $field_data['fields'] ) && ! empty( $field_data['fields'][ $field_inner ] ) ) {
|
||||
$field_data = $field_data['fields'][ $field_inner ];
|
||||
}
|
||||
|
||||
if ( empty( $field_data ) ) {
|
||||
wp_send_json( $r );
|
||||
}
|
||||
|
||||
$allowed_extensions = $field_data['mimes'];
|
||||
|
||||
if ( empty( $_FILES['file'] ) ) {
|
||||
wp_send_json(
|
||||
array(
|
||||
'error' => apply_filters( 'wpcfto_empty_file_error_label', __( 'Please, select file', 'nuxy' ) ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$path = ( isset( $_FILES['file']['name'] ) ) ? sanitize_text_field( $_FILES['file']['name'] ) : '';
|
||||
$ext = pathinfo( $path, PATHINFO_EXTENSION );
|
||||
|
||||
if ( ! in_array( $ext, $allowed_extensions, true ) ) {
|
||||
wp_send_json(
|
||||
array(
|
||||
'error' => true,
|
||||
'message' => apply_filters( 'wpcfto_file_error_ext_label', __( 'Invalid file extension', 'nuxy' ) ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$filename = md5( time() ) . basename( $path );
|
||||
$file = ( isset( $_FILES['file']['tmp_name'] ) ) ? file_get_contents( sanitize_text_field( $_FILES['file']['tmp_name'] ) ) : '';
|
||||
$upload_file = wp_upload_bits( $filename, null, $file );
|
||||
|
||||
if ( $upload_file['error'] ) {
|
||||
$r['error'] = $upload_file['error'];
|
||||
wp_send_json( $r );
|
||||
}
|
||||
|
||||
rename( $upload_file['file'], $this->get_file_path( $filename ) );
|
||||
|
||||
$file_data = $this->get_file_data( $filename );
|
||||
$r['error'] = '';
|
||||
$r['path'] = $file_data['path'];
|
||||
$r['url'] = $file_data['url'];
|
||||
|
||||
if ( apply_filters( "wpcfto_modify_file_{$field}", false ) ) {
|
||||
$r = apply_filters( "wpcfto_modified_{$field}", $r, $filename );
|
||||
}
|
||||
|
||||
wp_send_json( $r );
|
||||
}
|
||||
|
||||
public function get_file_path( $filename ) {
|
||||
return $this->upload_dir() . '/' . $filename;
|
||||
}
|
||||
|
||||
public function get_file_url( $filename ) {
|
||||
return $this->upload_url() . '/' . $filename;
|
||||
}
|
||||
|
||||
public function get_file_data( $filename ) {
|
||||
return array(
|
||||
'path' => $this->get_file_path( $filename ),
|
||||
'url' => $this->get_file_url( $filename ),
|
||||
);
|
||||
}
|
||||
|
||||
public function get_field_data( $name ) {
|
||||
$boxes = apply_filters( 'stm_wpcfto_fields', array() );
|
||||
|
||||
foreach ( $boxes as $box ) {
|
||||
foreach ( $box as $section ) {
|
||||
foreach ( $section['fields'] as $field_key => $field ) {
|
||||
if ( $field_key === $name ) {
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function upload_url() {
|
||||
$upload = wp_upload_dir();
|
||||
$upload_dir = $upload['baseurl'];
|
||||
|
||||
return $upload_dir . '/wpcfto_files';
|
||||
}
|
||||
|
||||
public static function upload_dir() {
|
||||
$upload = wp_upload_dir();
|
||||
$upload_dir = $upload['basedir'];
|
||||
|
||||
return $upload_dir . '/wpcfto_files';
|
||||
}
|
||||
|
||||
public function create_folder() {
|
||||
|
||||
global $wp_filesystem;
|
||||
|
||||
if ( empty( $wp_filesystem ) ) {
|
||||
require_once ABSPATH . '/wp-admin/includes/file.php';
|
||||
WP_Filesystem();
|
||||
}
|
||||
|
||||
$upload_dir = $this->upload_dir();
|
||||
|
||||
if ( ! $wp_filesystem->is_dir( $upload_dir ) ) {
|
||||
wp_mkdir_p( $upload_dir );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
156
vendor/wpcfto/helpers/helpers.php
vendored
Normal file
156
vendor/wpcfto/helpers/helpers.php
vendored
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
function stm_wpcfto_filtered_output( $data ) {
|
||||
return apply_filters( 'stm_wpcfto_filter_output', $data );
|
||||
}
|
||||
|
||||
function stm_wpcfto_is_pro() {
|
||||
return apply_filters( 'wpcfto_check_is_pro_field', false );
|
||||
}
|
||||
|
||||
function stm_wpcfto_wp_head() {
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
var stm_wpcfto_ajaxurl = '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>';
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.vue_is_disabled {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
|
||||
add_action( 'wp_head', 'stm_wpcfto_wp_head' );
|
||||
add_action( 'admin_head', 'stm_wpcfto_wp_head' );
|
||||
|
||||
function stm_wpcfto_nonces() {
|
||||
|
||||
$nonces = array(
|
||||
'wpcfto_save_settings',
|
||||
'get_image_url',
|
||||
'wpcfto_upload_file',
|
||||
'wpcfto_search_posts',
|
||||
'wpcfto_regenerate_fonts',
|
||||
);
|
||||
|
||||
$nonces_list = array();
|
||||
|
||||
foreach ( $nonces as $nonce_name ) {
|
||||
$nonces_list[ $nonce_name ] = wp_create_nonce( $nonce_name );
|
||||
}
|
||||
|
||||
?>
|
||||
<script>
|
||||
var stm_wpcfto_nonces = <?php echo wp_json_encode( $nonces_list ); ?>;
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
add_action( 'admin_head', 'stm_wpcfto_nonces' );
|
||||
add_action( 'wp_head', 'stm_wpcfto_nonces' );
|
||||
|
||||
add_action( 'wp_ajax_stm_wpcfto_get_settings', 'stm_wpcfto_get_settings_callback' );
|
||||
|
||||
function stm_wpcfto_get_settings_callback() {
|
||||
check_ajax_referer( 'stm_wpcfto_get_settings_nonce', 'nonce' );
|
||||
|
||||
$source = ( isset( $_GET['source'] ) ) ? sanitize_text_field( $_GET['source'] ) : '';
|
||||
$name = ( isset( $_GET['name'] ) ) ? sanitize_text_field( $_GET['name'] ) : '';
|
||||
wp_send_json( wpcfto_get_settings_map( $source, $name ) );
|
||||
}
|
||||
|
||||
function wpcfto_get_settings_map( $source, $name ) {
|
||||
if ( 'settings' === $source ) {
|
||||
$theme_options_page = array_merge(
|
||||
apply_filters( 'wpcfto_options_page_setup', array() ),
|
||||
apply_filters( 'wpcfto_get_frontend_settings', array() )
|
||||
);
|
||||
$settings_data = get_option( $name, array() );
|
||||
$settings = array();
|
||||
/*Get Our settings*/
|
||||
foreach ( $theme_options_page as $option_page ) {
|
||||
if ( $option_page['option_name'] !== $name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$settings = $option_page['fields'];
|
||||
}
|
||||
|
||||
$settings_data = apply_filters( 'filter_settings_data_values', $settings_data );
|
||||
|
||||
foreach ( $settings as $section_name => $section ) {
|
||||
foreach ( $section['fields'] as $field_name => $field ) {
|
||||
$default_value = ( ! empty( $field['value'] ) ) ? $field['value'] : '';
|
||||
$settings[ $section_name ]['fields'][ $field_name ]['value'] = ( isset( $settings_data[ $field_name ] ) ) ? $settings_data[ $field_name ] : $default_value;
|
||||
}
|
||||
}
|
||||
|
||||
return $settings;
|
||||
} else {
|
||||
$post_id = intval( $source );
|
||||
|
||||
$meta = STM_Metaboxes::convert_meta( $post_id );
|
||||
|
||||
$fields_data = apply_filters( 'stm_wpcfto_fields', array() );
|
||||
$sections = $fields_data[ $name ];
|
||||
|
||||
foreach ( $sections as $section_name => $section ) {
|
||||
foreach ( $section['fields'] as $field_name => $field ) {
|
||||
$default_value = ( ! empty( $field['value'] ) ) ? $field['value'] : '';
|
||||
$value = ( isset( $meta[ $field_name ] ) ) ? $meta[ $field_name ] : $default_value;
|
||||
if ( isset( $value ) ) {
|
||||
switch ( $field['type'] ) {
|
||||
case 'dates':
|
||||
if ( ! empty( $value ) ) {
|
||||
$value = explode( ',', $value );
|
||||
}
|
||||
break;
|
||||
case 'answers':
|
||||
if ( ! empty( $value ) ) {
|
||||
$value = unserialize( $value );
|
||||
}
|
||||
break;
|
||||
case 'repeater':
|
||||
if ( empty( $value ) ) {
|
||||
$value = array();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$sections[ $section_name ]['fields'][ $field_name ]['value'] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $sections;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function stm_wpcfto_get_options( $option_name, $option = '', $default_value = null ) {
|
||||
$options = get_option( $option_name, array() );
|
||||
|
||||
if ( empty( $option ) ) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
return isset( $options[ $option ] ) ? $options[ $option ] : $default_value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
add_action( 'wp_ajax_wpcfto_get_image_url', 'wpcfto_get_image_url' );
|
||||
|
||||
function wpcfto_get_image_url() {
|
||||
if ( empty( $_GET['image_id'] ) ) {
|
||||
die;
|
||||
}
|
||||
wp_send_json( wp_get_attachment_url( intval( $_GET['image_id'] ) ) );
|
||||
}
|
||||
|
||||
function wpcfto_sanitize_string( $taxonomy ) {
|
||||
return apply_filters( 'wpcfto_sanitize_string', urldecode( sanitize_title( urldecode( $taxonomy ) ) ), $taxonomy );
|
||||
}
|
||||
26
vendor/wpcfto/helpers/icons.php
vendored
Normal file
26
vendor/wpcfto/helpers/icons.php
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user