/** * Save and reuse the results of compiled less files. * The first call to Get() will generate css and save it. * Subsequent calls to Get() with the same arguments will return the same css filename * * @param array $less_files Array of .less files to compile * @param array $parser_options Array of compiler options * @param array $modify_vars Array of variables * @return string Name of the css file */ public static function Get($less_files, $parser_options = array(), $modify_vars = array()) { //check $cache_dir if (isset($parser_options['cache_dir'])) { Less_Cache::$cache_dir = $parser_options['cache_dir']; } if (empty(Less_Cache::$cache_dir)) { throw new Exception('cache_dir not set'); } self::CheckCacheDir(); $less_files = (array) $less_files; //create a file for variables if (!empty($modify_vars)) { $lessvars = Less_Parser::serializeVars($modify_vars); $vars_file = Less_Cache::$cache_dir . 'lessphpvars_' . sha1($lessvars) . '.less'; if (!file_exists($vars_file)) { thim_file_put_contents($vars_file, $lessvars); } $less_files += array($vars_file => '/'); } // generate name for compiled css file $hash = md5(json_encode($less_files)); $list_file = Less_Cache::$cache_dir . 'lessphp_' . $hash . '.list'; // check cached content if (!isset($parser_options['use_cache']) || $parser_options['use_cache'] === true) { if (file_exists($list_file)) { self::ListFiles($list_file, $list, $cached_name); $compiled_name = self::CompiledName($list); // if $cached_name != $compiled_name, we know we need to recompile if (!$cached_name || $cached_name === $compiled_name) { $output_file = self::OutputFile($compiled_name, $parser_options); if ($output_file && file_exists($output_file)) { @touch($list_file); return basename($output_file); // for backwards compatibility, we just return the name of the file } } } } $compiled = self::Cache($less_files, $parser_options); if (!$compiled) { return false; } $compiled_name = self::CompiledName($less_files); $output_file = self::OutputFile($compiled_name, $parser_options); //save the file list $list = $less_files; $list[] = $compiled_name; $cache = implode("\n", $list); thim_file_put_contents($list_file, $cache); //save the css thim_file_put_contents($output_file, $compiled); //clean up self::CleanCache(); return basename($output_file); }
if (!$file_content) { $response = array('step' => 'download', 'next' => 'extract', 'return' => false, 'message' => sprintf(__('Cannot get demo data package from %', 'thim'), $package_url)); echo json_encode($response); exit; } $upload_dir = wp_upload_dir(); $demodata_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'tp-datademo' . DIRECTORY_SEPARATOR . $demodata; $_SESSION['thimpress-demodata-dir'] = $demodata_dir; $_SESSION['thimpress-demodata-file'] = $filename; $_SESSION['thimpress-demodata-downloaded'] = false; if (!wp_mkdir_p($demodata_dir)) { $response = array('step' => 'download', 'next' => 'extract', 'return' => false, 'message' => sprintf(__('Cannot create directory "%"', 'thim'), $demodata_dir)); echo json_encode($response); exit; } if (!thim_file_put_contents($demodata_dir . DIRECTORY_SEPARATOR . $filename, $file_content)) { $response = array('step' => 'download', 'next' => 'extract', 'return' => false, 'message' => sprintf(__('Cannot store file "%s"', 'thim'), $demodata_dir . DIRECTORY_SEPARATOR . $filename)); echo json_encode($response); exit; } $log = ob_end_clean(); $_SESSION['thimpress-demodata-downloaded'] = true; $response = array('step' => 'download', 'next' => 'extract', 'return' => true, 'message' => sprintf(__('Download demo data package success "%s"', 'thim'), $demodata_dir . DIRECTORY_SEPARATOR . $filename), 'log' => $log); echo json_encode($response); exit; } } elseif ($type == 'extract') { ob_start(); $file_zip = preg_replace('/[\\/]/i', DIRECTORY_SEPARATOR, $_SESSION['thimpress-demodata-dir'] . DIRECTORY_SEPARATOR . $_SESSION['thimpress-demodata-file']); $unzip_dir = dirname($file_zip); WP_Filesystem();
public function compileFile($fname, $outFname = null) { if (!is_readable($fname)) { throw new Exception('load error: failed to find ' . $fname); } $pi = pathinfo($fname); $oldImport = $this->importDir; $this->importDir = (array) $this->importDir; $this->importDir[] = realpath($pi['dirname']) . '/'; $this->allParsedFiles = array(); $this->addParsedFile($fname); $parser = new Less_Parser(); $parser->SetImportDirs($this->getImportDirs()); if (count($this->registeredVars)) { $parser->ModifyVars($this->registeredVars); } foreach ($this->libFunctions as $name => $func) { $parser->registerFunction($name, $func); } $parser->parseFile($fname); $out = $parser->getCss(); $parsed = Less_Parser::AllParsedFiles(); foreach ($parsed as $file) { $this->addParsedFile($file); } $this->importDir = $oldImport; if ($outFname !== null) { return thim_file_put_contents($outFname, $out); } return $out; }
/** * Create new posts based on import information * * Posts marked as having a parent which doesn't exist will become top level items. * Doesn't create a new post if: the post type doesn't exist, the given post ID * is already noted as imported or a post with the same title and date already exists. * Note that new/updated terms, comments and meta are imported for the last of the above. */ function process_posts() { global $wpdb; $this->posts = apply_filters('wp_import_posts', $this->posts); @($start = intval(thim_file_get_contents(POST_COUNT))); $end = 0; if ($start < 1) { $start = 0; $end = 50; } if ($end > count($this->posts)) { $end = count($this->posts); } else { $end = 50; } $i = 0; $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID\n\t\t\t\t\tFROM {$wpdb->posts} as wposts, {$wpdb->postmeta} as wpostmeta\n\t\t\t\t\t\tWHERE wposts.ID = wpostmeta.post_id\n\t\t\t\t\t\tAND wpostmeta.meta_key = %s\n\t\t\t\t\t\tAND wpostmeta.meta_value LIKE %s\n\t\t\t\t\t\tAND wposts.post_type = %s\n\t\t\t\t\tORDER BY wpostmeta.meta_id ASC", '_wp_attached_file', '%demo_image.jpg', 'attachment')); foreach ($this->posts as $k => $post) { if ($k <= $start) { continue; } if ($i >= $end) { return 0; } thim_file_put_contents(POST_COUNT, $k); file_put_contents(POST_COUNT_TEST, "current_position:{$k} - start: {$start} - jump:{$end} - post_imported: {$i} - data: " . json_encode($post) . "\n", FILE_APPEND); $post = apply_filters('wp_import_post_data_raw', $post); if ('attachment' == $post['post_type'] && $attachment_id) { $this->processed_posts[intval($post['post_id'])] = (int) $attachment_id; $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } continue; } if (!post_type_exists($post['post_type'])) { do_action('wp_import_post_exists', $post); continue; } if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) { continue; } if ($post['status'] == 'auto-draft') { continue; } if ('nav_menu_item' == $post['post_type']) { $this->process_menu_item($post); $i++; continue; } $post_exists = post_exists($post['post_title'], '', $post['post_date']); if ($post_exists && get_post_type($post_exists) == $post['post_type']) { $comment_post_ID = $post_id = $post_exists; } else { $post_parent = (int) $post['post_parent']; if ($post_parent) { // if we already know the parent, map it to the new local ID if (isset($this->processed_posts[$post_parent])) { $post_parent = $this->processed_posts[$post_parent]; // otherwise record the parent for later } else { $this->post_orphans[intval($post['post_id'])] = $post_parent; $post_parent = 0; } } // map the post author $author = sanitize_user($post['post_author'], true); if (isset($this->author_mapping[$author])) { $author = $this->author_mapping[$author]; } else { $author = (int) get_current_user_id(); } $postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']); $original_post_ID = $post['post_id']; $postdata = apply_filters('wp_import_post_data_processed', $postdata, $post); if ('attachment' == $postdata['post_type']) { if (!$attachment_id) { $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID\n\t\t\t\t\t\t\t\t\tFROM {$wpdb->posts} as wposts, {$wpdb->postmeta} as wpostmeta\n\t\t\t\t\t\t\t\t\t\tWHERE wposts.ID = wpostmeta.post_id\n\t\t\t\t\t\t\t\t\t\tAND wpostmeta.meta_key = %s\n\t\t\t\t\t\t\t\t\t\tAND wpostmeta.meta_value LIKE %s\n\t\t\t\t\t\t\t\t\t\tAND wposts.post_type = %s\n\t\t\t\t\t\t\t\t\tORDER BY wpostmeta.meta_id ASC ", '_wp_attached_file', '%demo_image.jpg', 'attachment')); } if (!$attachment_id) { $remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; if (!$this->method) { $array = array($remote_url); $remote_url = $this->ww_replace_image_links_with_local($array); $remote_url = $remote_url[0]; } // try to use _wp_attached file for upload folder placement to ensure the same location as the export site // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() $postdata['upload_date'] = $post['post_date']; if (isset($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { if ($meta['key'] == '_wp_attached_file') { if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { $postdata['upload_date'] = $matches[0]; } break; } } } $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); } else { $comment_post_ID = $post_id = $attachment_id; } } else { $comment_post_ID = $post_id = wp_insert_post($postdata, true); do_action('wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post); } if (is_wp_error($post_id)) { if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { } continue; } if ($post['is_sticky'] == 1) { stick_post($post_id); } } // map pre-import ID to local ID $this->processed_posts[intval($post['post_id'])] = (int) $post_id; if ($attachment_id && 'attachment' == $post['post_type']) { continue; } if (!isset($post['terms'])) { $post['terms'] = array(); } $post['terms'] = apply_filters('wp_import_post_terms', $post['terms'], $post_id, $post); // add categories, tags and other terms if (!empty($post['terms'])) { $terms_to_set = array(); foreach ($post['terms'] as $term) { // back compat with WXR 1.0 map 'tag' to 'post_tag' $taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain']; $term_exists = term_exists($term['slug'], $taxonomy); $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; if (!$term_id) { $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); if (!is_wp_error($t)) { $term_id = $t['term_id']; do_action('wp_import_insert_term', $t, $term, $post_id, $post); } else { if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { } do_action('wp_import_insert_term_failed', $t, $term, $post_id, $post); continue; } } $terms_to_set[$taxonomy][] = intval($term_id); } foreach ($terms_to_set as $tax => $ids) { $tt_ids = wp_set_post_terms($post_id, $ids, $tax); do_action('wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post); } unset($post['terms'], $terms_to_set); } if (!isset($post['comments'])) { $post['comments'] = array(); } $post['comments'] = apply_filters('wp_import_post_comments', $post['comments'], $post_id, $post); // add/update comments if (!empty($post['comments'])) { $num_comments = 0; $inserted_comments = array(); foreach ($post['comments'] as $comment) { $comment_id = $comment['comment_id']; $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); if (isset($this->processed_authors[$comment['comment_user_id']])) { $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; } } ksort($newcomments); foreach ($newcomments as $key => $comment) { // if this is a new post we can skip the comment_exists() check if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) { if (isset($inserted_comments[$comment['comment_parent']])) { $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; } $comment = wp_filter_comment($comment); $inserted_comments[$key] = wp_insert_comment($comment); do_action('wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post); foreach ($comment['commentmeta'] as $meta) { $value = maybe_unserialize($meta['value']); add_comment_meta($inserted_comments[$key], $meta['key'], $value); } $num_comments++; } } unset($newcomments, $inserted_comments, $post['comments']); } if (!isset($post['postmeta'])) { $post['postmeta'] = array(); } $post['postmeta'] = apply_filters('wp_import_post_meta', $post['postmeta'], $post_id, $post); // add/update post meta if (!empty($post['postmeta'])) { foreach ($post['postmeta'] as $meta) { $key = apply_filters('import_post_meta_key', $meta['key'], $post_id, $post); $value = false; if ('_edit_last' == $key) { if (isset($this->processed_authors[intval($meta['value'])])) { $value = $this->processed_authors[intval($meta['value'])]; } else { $key = false; } } if ($key) { // export gets meta straight from the DB so could have a serialized string if (!$value) { $value = maybe_unserialize($meta['value']); } if (!$this->method) { // FIX IMAGES URL'S AND UPLOAD LOCAL IMAGES $value = $this->ww_replace_image_links_with_local($value); } add_post_meta($post_id, $key, $value); do_action('import_post_meta', $post_id, $key, $value); // if the post has a featured image, take note of this in case of remap if ('_thumbnail_id' == $key) { $value = $this->ww_replace_image_links_with_local($value, false, true); } } } } $i++; } unset($this->posts); return true; }
/** * */ function import_revslider() { @($data_rev = thim_file_get_contents(REV_IMPORT)); if ($handle = opendir(TP_THEME_DIR . "inc/admin/data/revslider")) { $check = 0; while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $_FILES['import_file']['tmp_name'] = TP_THEME_DIR . "inc/admin/data/revslider/" . $entry; if ($data_rev == $entry) { $check = 1; continue; } if (!$data_rev) { $check = 1; } if (class_exists('RevSlider')) { if ($check) { $slider = new RevSlider(); $response = $slider->importSliderFromPost(true, true); thim_file_put_contents(REV_IMPORT, $entry); return 1; } } else { return false; } } } closedir($handle); } else { return false; } unlink(REV_IMPORT); return 2; }
/** * Compile .scss file * * @param string $in Input path (.scss) * @param string $out Output path (.css) * * @return string */ protected function compile($in, $out) { $start = microtime(true); $css = $this->scss->compile(thim_file_get_contents($in), $in); $elapsed = round(microtime(true) - $start, 4); $v = scssc::$VERSION; $t = date('r'); $css = "/* compiled by scssphp {$v} on {$t} ({$elapsed}s) */\n\n" . $css; thim_file_put_contents($out, $css); thim_file_put_contents($this->importsCacheName($out), serialize($this->scss->getParsedFiles())); return $css; }
/** * Saves the source map to a file * * @param string $file The absolute path to a file * @param string $content The content to write * @throws Exception If the file could not be saved */ protected function saveMap($file, $content) { $dir = dirname($file); // directory does not exist if (!is_dir($dir)) { // FIXME: create the dir automatically? throw new Exception(sprintf('The directory "%s" does not exist. Cannot save the source map.', $dir)); } // FIXME: proper saving, with dir write check! if (thim_file_put_contents($file, $content) === false) { throw new Exception(sprintf('Cannot save the source map to "%s"', $file)); } return true; }
/** * Return the results of parsePrimary for $file_path * Use cache and save cached results if possible * * @param string|null $file_path */ private function GetRules($file_path) { $this->SetInput($file_path); $cache_file = $this->CacheFile($file_path); if ($cache_file) { if (Less_Parser::$options['cache_method'] == 'callback') { if (is_callable(Less_Parser::$options['cache_callback_get'])) { $cache = call_user_func_array(Less_Parser::$options['cache_callback_get'], array($this, $file_path, $cache_file)); if ($cache) { $this->UnsetInput(); return $cache; } } } elseif (file_exists($cache_file)) { switch (Less_Parser::$options['cache_method']) { // Using serialize // Faster but uses more memory case 'serialize': $cache = unserialize(thim_file_get_contents($cache_file)); if ($cache) { touch($cache_file); $this->UnsetInput(); return $cache; } break; // Using generated php code // Using generated php code case 'var_export': case 'php': $this->UnsetInput(); return include $cache_file; } } } $rules = $this->parsePrimary(); if ($this->pos < $this->input_len) { throw new Less_Exception_Chunk($this->input, null, $this->furthest, $this->env->currentFileInfo); } $this->UnsetInput(); //save the cache if ($cache_file) { if (Less_Parser::$options['cache_method'] == 'callback') { if (is_callable(Less_Parser::$options['cache_callback_set'])) { call_user_func_array(Less_Parser::$options['cache_callback_set'], array($this, $file_path, $cache_file, $rules)); } } else { switch (Less_Parser::$options['cache_method']) { case 'serialize': thim_file_put_contents($cache_file, serialize($rules)); break; case 'php': thim_file_put_contents($cache_file, '<?php return ' . self::ArgString($rules) . '; ?>'); break; case 'var_export': thim_file_put_contents($cache_file, '<?php return ' . var_export($rules, true) . '; ?>'); break; } Less_Cache::CleanCache(); } } return $rules; }