예제 #1
0
 /**
  * Plupload action.
  * 
  * @since 1.0.0
  * @access public
  * @return json
  */
 public function plupload()
 {
     $imgid = $_POST['imgid'];
     check_ajax_referer($this->action_nonce, $this->field_nonce);
     /** Decide whether to send this image to Media. @var String */
     $add_to_media_library = isset($_POST['tomedia']) ? $_POST['tomedia'] : false;
     /** If post ID is set, uploaded image will be attached to it. @var String */
     $postid = isset($_POST['topost']) ? intval($_POST['topost']) : '';
     $import_type = isset($_POST['import_type']) ? $_POST['import_type'] : 'import';
     $method = isset($_POST['import_method']) ? $_POST['import_method'] : 'add';
     $import_source = isset($_POST['import_source']) ? $_POST['import_source'] : 'theme';
     /** Handle file upload storing file|url|type. @var Array */
     $file = wp_handle_upload($_FILES[$imgid . 'async-upload'], array('test_form' => true, 'action' => 'tf_plupload'));
     if ($file && !isset($file['error'])) {
         //let's see if it's an image, a zip file or something else
         $ext = explode('/', $file['type']);
         // Import routines
         $allowed_types = array('zip', 'rar', 'plain', 'xml');
         if (in_array($ext[1], $allowed_types)) {
             $url = wp_nonce_url('admin.php?page=themify-flow');
             if (false === ($creds = request_filesystem_credentials($url))) {
                 return true;
             }
             if (!WP_Filesystem($creds)) {
                 request_filesystem_credentials($url, '', true);
                 return true;
             }
             global $wp_filesystem, $TF, $tf_export;
             if ('zip' == $ext[1] || 'rar' == $ext[1]) {
                 unzip_file($file['file'], $TF->framework_path());
                 $file_meta = $tf_export->get_filename_data('tf_' . $import_source);
                 $filename = $TF->framework_path() . '/' . sanitize_file_name($file_meta['file']);
             } else {
                 $filename = $file['file'];
             }
             if ($wp_filesystem->exists($filename)) {
                 if ('content_builder' == $import_source) {
                     $import = new TF_Import_Content_Builder();
                     $import->content_builder_id = $postid;
                     $import->fetch_attachments = true;
                     $import->import($filename);
                 } else {
                     $import = new TF_Import();
                     $import->fetch_attachments = true;
                     $import->method = $method;
                     $import->source = $import_source;
                     if ('edit' == $method) {
                         $import->edit_import_id = $postid;
                     }
                     $import->import($filename);
                     if ($import->fails()) {
                         $file['error'] = implode('\\n', $import->get_error_messages());
                     } else {
                         $file['activate_theme_uri'] = wp_nonce_url(admin_url('post.php?post=' . $import->return_ID . '&action=activate_tf_theme'), 'tf_theme_nonce');
                     }
                 }
                 $wp_filesystem->delete($filename);
                 $wp_filesystem->delete($file['file']);
             } else {
                 $file['error'] = __('Data could not be loaded because import data not founded in the zip.', 'themify-flow');
                 // Delete dump file
                 foreach ($tf_export->file_names as $data_file) {
                     if ($wp_filesystem->exists($TF->framework_path() . '/' . $data_file['file'])) {
                         $wp_filesystem->delete($TF->framework_path() . '/' . $data_file['file']);
                     }
                 }
             }
         } else {
             //Image Upload routines
             if ('tomedia' == $add_to_media_library) {
                 // Insert into Media Library
                 // Set up options array to add this file as an attachment
                 $attachment = array('post_mime_type' => sanitize_mime_type($file['type']), 'post_title' => str_replace('-', ' ', sanitize_file_name(pathinfo($file['file'], PATHINFO_FILENAME))), 'post_status' => 'inherit');
                 if ($postid) {
                     $attach_id = wp_insert_attachment($attachment, $file['file'], $postid);
                 } else {
                     $attach_id = wp_insert_attachment($attachment, $file['file']);
                 }
                 // Common attachment procedures
                 require_once ABSPATH . "wp-admin" . '/includes/image.php';
                 $attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
                 wp_update_attachment_metadata($attach_id, $attach_data);
                 if ($postid) {
                     $full = wp_get_attachment_image_src($attach_id, 'full');
                     if ($_POST['featured']) {
                         //Set the featured image for the post
                         set_post_thumbnail($postid, $attach_id);
                     }
                     update_post_meta($postid, $_POST['fields'], $full[0]);
                     update_post_meta($postid, '_' . $_POST['fields'] . '_attach_id', $attach_id);
                     $thumb = wp_get_attachment_image_src($attach_id, 'thumbnail');
                     //Return URL for the image field in meta box
                     $file['thumb'] = $thumb[0];
                 }
             }
         }
         $file['type'] = $ext[1];
     }
     // send the uploaded file url in response
     echo json_encode($file);
     exit;
 }
예제 #2
0
 /**
  * Import theme files 
  * Using Example flow_import(array('source1'=>array('method'=>'import','type'=>'theme','activate'=>false),
  *                                 'source2'=>array('method'=>'edit','type'=>'template_part'))
  *                          );
  * @since  1.0.0
  * @access public
  * @param  array $sources 
  * @return mixed
  */
 public static function flow_import(array $sources)
 {
     if (empty($sources)) {
         return false;
     }
     include_once 'classes/import-export/class-tf-import.php';
     include_once ABSPATH . 'wp-admin/includes/file.php';
     include_once ABSPATH . 'wp-admin/includes/image.php';
     WP_Filesystem();
     global $wp_filesystem, $TF;
     $errors = array();
     foreach ($sources as $path => $data) {
         if (!isset($data['method']) || !isset($data['type']) || !in_array($data['type'], array('theme', 'template', 'template_part')) || !in_array($data['method'], array('import', 'replace')) || !$wp_filesystem->exists($path)) {
             continue;
         }
         $file = wp_check_filetype($path);
         if (!isset($file['ext']) || !in_array($file['ext'], array('plain', 'rar', 'zip', 'xml'))) {
             continue;
         }
         $is_zip = FALSE;
         if ($file['ext'] == 'zip' || $file['ext'] == 'rar') {
             $tmp_dir = sys_get_temp_dir() . '/' . uniqid('flow') . '/';
             $unzipfile = unzip_file($path, $tmp_dir);
             if (!$unzipfile) {
                 $errors[$path] = 'There was an error unzipping the file ' . basename($path);
                 continue;
             }
             $tmp_file = scandir($tmp_dir);
             if (empty($tmp_file)) {
                 $errors[$path] = 'There was an error unzipping the file ' . basename($path);
                 continue;
             }
             $xml_file = false;
             foreach ($tmp_file as $tmp) {
                 if ($tmp != '.' && $tmp != '..' && $wp_filesystem->exists($tmp_dir . $tmp)) {
                     $xml_file = $tmp;
                     break;
                 }
             }
             if (!$xml_file) {
                 $errors[$path] = 'There was an error unzipping the file ' . basename($path);
                 continue;
             }
             $xml = wp_check_filetype($tmp_dir . $xml_file, array('xml' => 'application/xml'));
             if ($xml['ext'] != 'xml') {
                 $errors[$path] = 'The ' . $file['ext'] . ' file ' . basename($path) . ' doesn`t contain xml file';
                 continue;
             }
             $is_zip = true;
             $path = $tmp_dir . $xml_file;
         }
         $import = new TF_Import();
         $posts = $import->parse($path);
         remove_filter('wp_unique_post_slug', array($TF->active_theme, 'add_prefix_post_slug'), 10, 6);
         if (isset($posts['posts']) && !empty($posts['posts'])) {
             $import->method = $data['method'] == 'import' ? 'add' : 'edit';
             $import->source = $data['type'];
             $import->fetch_attachments = true;
             $import->edit_import_id = FALSE;
             $import->set_associated_theme_by_value = true;
             $errors[$path] = array();
             if ($data['type'] == 'theme') {
                 $import->exclude_theme_post = FALSE;
                 if ($import->method == 'add') {
                     $import->import($path);
                 } else {
                     foreach ($posts['posts'] as $post) {
                         if ($post['post_type'] == 'tf_' . $data['type']) {
                             $post_exists = TF_Model::post_exists($post['post_name']);
                             if ($post_exists && get_post_type($post_exists) == $post['post_type']) {
                                 $import->edit_import_id = $post_exists;
                                 $import->import($path);
                             } else {
                                 $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' doesn`t exist.';
                             }
                             break;
                         }
                     }
                 }
                 if (isset($data['activate']) && $data['activate'] && !$import->fails()) {
                     $activate = $import->return_ID > 0 ? $import->return_ID : $import->edit_import_id;
                 }
             } else {
                 $import->exclude_theme_post = TRUE;
                 $import->set_associated_theme_by_value = true;
                 $import->get_authors_from_import($posts);
                 $import->get_author_mapping();
                 wp_defer_term_counting(true);
                 wp_defer_comment_counting(true);
                 do_action('tf_import_start');
                 foreach ($posts['posts'] as $post) {
                     if ('tf_' . $data['type'] == $post['post_type']) {
                         $post_exists = TF_Model::post_exists($post['post_name']);
                         $post_type = $post_exists ? get_post_type($post_exists) == $post['post_type'] : FALSE;
                         $import->posts = array($post);
                         if ($import->method == 'add') {
                             if ($post_exists && $post_type) {
                                 $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' already exist.';
                             } else {
                                 $import->processed_posts();
                             }
                         } elseif ($post_exists && $post_type) {
                             $import->edit_import_id = $post_exists;
                             $import->process_replace_posts();
                         } else {
                             $errors[$path][] = $post['post_title'] . ' ' . $data['type'] . ' doesn`t exists.';
                         }
                     }
                 }
             }
             if ($is_zip) {
                 $wp_filesystem->delete($path);
             }
             $messages = $import->get_error_messages();
             if (!empty($messages)) {
                 foreach ($messages as $m) {
                     $errors[$path][] = $m;
                 }
             } elseif (empty($errors[$path])) {
                 unset($errors[$path]);
             }
         }
     }
     if (isset($activate) && $activate) {
         global $TF_Theme, $TF;
         $TF_Theme->set_active_theme($activate);
         $TF->active_theme = new TF_Engine_Theme_Loader();
         do_action('tf_import_end');
     }
     return empty($errors) ? TRUE : $errors;
 }
 /**
  * Import base theme data.
  * 
  * @since 1.0.0
  * @access private
  */
 private function import_base_theme()
 {
     global $TF;
     $zip_file = $TF->framework_path() . '/includes/data/theme-base.zip';
     $filename = $TF->framework_path() . '/theme_export.xml';
     if (!function_exists('WP_Filesystem')) {
         require_once ABSPATH . 'wp-admin/includes/file.php';
     }
     WP_Filesystem();
     global $wp_filesystem;
     if ($wp_filesystem->exists($zip_file)) {
         unzip_file($zip_file, $TF->framework_path());
         if ($wp_filesystem->exists($filename)) {
             // Remove function hooked in class-tf-engine-style-loader.php
             remove_action('tf_import_end', array('TF_Model', 'create_stylesheets'));
             add_action('tf_import_end', array($this, 'set_initial_active_theme'));
             $import = new TF_Import();
             $import->fetch_attachments = true;
             $import->base_theme = true;
             $import->import($filename);
             $wp_filesystem->delete($filename);
         }
     }
 }
 /**
  * Save form post data via Hooks
  * 
  * @since 1.0.0
  * @access public
  * @param array $post_data 
  */
 public function saving_form($post_data)
 {
     global $TF;
     $post_data = wp_parse_args($post_data, $this->defaults);
     if ('edit' == $post_data['tf_theme_form_state']) {
         $new_id = $post_data['_theme_id'];
         $new_post = array();
         $new_post['ID'] = $new_id;
         $new_post['post_title'] = sanitize_text_field($post_data['tf_theme_name']);
         wp_update_post($new_post);
     } else {
         // Create post object
         $my_post = array('post_title' => sanitize_text_field($post_data['tf_theme_name']), 'post_status' => 'publish', 'post_type' => $this->post_type);
         $my_post['post_name'] = str_replace('-', '_', sanitize_title($my_post['post_title']));
         // Insert the post into the database
         $new_id = wp_insert_post($my_post);
     }
     if ($new_id) {
         unset($post_data['tf_theme_name']);
         if ('' != $post_data['tf_theme_screenshot'] && '' != $post_data['tf_theme_screenshot_attach_id']) {
             set_post_thumbnail($new_id, $post_data['tf_theme_screenshot_attach_id']);
         }
         update_post_meta($new_id, 'theme_info', $post_data);
         // Importing templates and parts if checked
         if ('add' == $post_data['tf_theme_form_state'] && false !== $post_data['tf_import_base_template_and_part']) {
             $zip_file = $TF->framework_path() . '/includes/data/theme-base.zip';
             $filename = $TF->framework_path() . '/theme_export.xml';
             if (!function_exists('WP_Filesystem')) {
                 require_once ABSPATH . 'wp-admin/includes/file.php';
             }
             WP_Filesystem();
             global $wp_filesystem;
             if ($wp_filesystem->exists($zip_file)) {
                 unzip_file($zip_file, $TF->framework_path());
                 if ($wp_filesystem->exists($filename)) {
                     // Remove function hooked in class-tf-engine-style-loader.php
                     remove_action('tf_import_end', array('TF_Model', 'create_stylesheets'));
                     $query_post = get_post($new_id);
                     $import = new TF_Import();
                     $import->fetch_attachments = true;
                     $import->exclude_theme_post = true;
                     $import->set_associated_theme = $query_post->post_name;
                     $import->import($filename);
                     $wp_filesystem->delete($filename);
                 }
             }
         }
         // Return activate url
         $return_url = wp_nonce_url(admin_url('post.php?post=' . $new_id . '&action=activate_tf_theme'), 'tf_theme_nonce');
         $new_theme = 'add' == $post_data['tf_theme_form_state'] ? true : false;
         wp_send_json_success(array('newTheme' => $new_theme, 'url' => $return_url));
     }
 }