/** * Delete a site. * * @since 3.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $blog_id Site ID. * @param bool $drop True if site's database tables should be dropped. Default is false. */ function wpmu_delete_blog($blog_id, $drop = false) { global $wpdb; $switch = false; if (get_current_blog_id() != $blog_id) { $switch = true; switch_to_blog($blog_id); } $blog = get_blog_details($blog_id); /** * Fires before a site is deleted. * * @since MU * * @param int $blog_id The site ID. * @param bool $drop True if site's table should be dropped. Default is false. */ do_action('delete_blog', $blog_id, $drop); $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ids')); // Remove users from this blog. if (!empty($users)) { foreach ($users as $user_id) { remove_user_from_blog($user_id, $blog_id); } } update_blog_status($blog_id, 'deleted', 1); $current_site = get_current_site(); // If a full blog object is not available, do not destroy anything. if ($drop && !$blog) { $drop = false; } // Don't destroy the initial, main, or root blog. if ($drop && (1 == $blog_id || is_main_site($blog_id) || $blog->path == $current_site->path && $blog->domain == $current_site->domain)) { $drop = false; } $upload_path = trim(get_option('upload_path')); // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable. if ($drop && get_site_option('ms_files_rewriting') && empty($upload_path)) { $drop = false; } if ($drop) { $uploads = wp_get_upload_dir(); $tables = $wpdb->tables('blog'); /** * Filters the tables to drop when the site is deleted. * * @since MU * * @param array $tables The site tables to be dropped. * @param int $blog_id The ID of the site to drop tables for. */ $drop_tables = apply_filters('wpmu_drop_tables', $tables, $blog_id); foreach ((array) $drop_tables as $table) { $wpdb->query("DROP TABLE IF EXISTS `{$table}`"); } $wpdb->delete($wpdb->blogs, array('blog_id' => $blog_id)); /** * Filters the upload base directory to delete when the site is deleted. * * @since MU * * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir() * @param int $blog_id The site ID. */ $dir = apply_filters('wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id); $dir = rtrim($dir, DIRECTORY_SEPARATOR); $top_dir = $dir; $stack = array($dir); $index = 0; while ($index < count($stack)) { // Get indexed directory from stack $dir = $stack[$index]; $dh = @opendir($dir); if ($dh) { while (($file = @readdir($dh)) !== false) { if ($file == '.' || $file == '..') { continue; } if (@is_dir($dir . DIRECTORY_SEPARATOR . $file)) { $stack[] = $dir . DIRECTORY_SEPARATOR . $file; } elseif (@is_file($dir . DIRECTORY_SEPARATOR . $file)) { @unlink($dir . DIRECTORY_SEPARATOR . $file); } } @closedir($dh); } $index++; } $stack = array_reverse($stack); // Last added dirs are deepest foreach ((array) $stack as $dir) { if ($dir != $top_dir) { @rmdir($dir); } } clean_blog_cache($blog); } if ($switch) { restore_current_blog(); } }
/** * Tries to convert an attachment URL into a post ID. * * @since 4.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $url The URL to resolve. * @return int The found post ID, or 0 on failure. */ function attachment_url_to_postid($url) { global $wpdb; $dir = wp_get_upload_dir(); $path = $url; $site_url = parse_url($dir['url']); $image_path = parse_url($path); //force the protocols to match if needed if (isset($image_path['scheme']) && $image_path['scheme'] !== $site_url['scheme']) { $path = str_replace($image_path['scheme'], $site_url['scheme'], $path); } if (0 === strpos($path, $dir['baseurl'] . '/')) { $path = substr($path, strlen($dir['baseurl'] . '/')); } $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path); $post_id = $wpdb->get_var($sql); /** * Filters an attachment id found by URL. * * @since 4.2.0 * * @param int|null $post_id The post_id (if any) found by the function. * @param string $url The URL being looked up. */ return (int) apply_filters('attachment_url_to_postid', $post_id, $url); }
/** * Retrieve the URL for an attachment. * * @since 2.1.0 * * @global string $pagenow * * @param int $post_id Optional. Attachment ID. Default 0. * @return string|false Attachment URL, otherwise false. */ function wp_get_attachment_url($post_id = 0) { $post_id = (int) $post_id; if (!($post = get_post($post_id))) { return false; } if ('attachment' != $post->post_type) { return false; } $url = ''; // Get attached file. if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) { // Get upload directory. if (($uploads = wp_get_upload_dir()) && false === $uploads['error']) { // Check that the upload base exists in the file location. if (0 === strpos($file, $uploads['basedir'])) { // Replace file location with url location. $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); } elseif (false !== strpos($file, 'wp-content/uploads')) { // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) $url = trailingslashit($uploads['baseurl'] . '/' . _wp_get_attachment_relative_path($file)) . basename($file); } else { // It's a newly-uploaded file, therefore $file is relative to the basedir. $url = $uploads['baseurl'] . "/{$file}"; } } } /* * If any of the above options failed, Fallback on the GUID as used pre-2.7, * not recommended to rely upon this. */ if (empty($url)) { $url = get_the_guid($post->ID); } // On SSL front-end, URLs should be HTTPS. if (is_ssl() && !is_admin() && 'wp-login.php' !== $GLOBALS['pagenow']) { $url = set_url_scheme($url); } /** * Filter the attachment URL. * * @since 2.1.0 * * @param string $url URL for the given attachment. * @param int $post_id Attachment ID. */ $url = apply_filters('wp_get_attachment_url', $url, $post->ID); if (empty($url)) { return false; } return $url; }
/** * Returns the base URL of the uploads directory. * Note: this function will be removed in 4.6. * * @ignore * @since 4.4.0 * @access private * @deprecated 4.5.0 Use wp_get_upload_dir() * @see wp_get_upload_dir() * * @return string The base URL. */ function _wp_upload_dir_baseurl() { _deprecated_function(__FUNCTION__, '4.5', 'wp_get_upload_dir()'); $upload_dir = wp_get_upload_dir(); return $upload_dir['baseurl']; }
/** * Finds a pingback server URI based on the given URL. * * Checks the HTML for the rel="pingback" link and x-pingback headers. It does * a check for the x-pingback headers first and returns that, if available. The * check for the rel="pingback" has more overhead than just the header. * * @since 1.5.0 * * @param string $url URL to ping. * @param int $deprecated Not Used. * @return false|string False on failure, string containing URI on success. */ function discover_pingback_server_uri($url, $deprecated = '') { if (!empty($deprecated)) { _deprecated_argument(__FUNCTION__, '2.7'); } $pingback_str_dquote = 'rel="pingback"'; $pingback_str_squote = 'rel=\'pingback\''; /** @todo Should use Filter Extension or custom preg_match instead. */ $parsed_url = parse_url($url); if (!isset($parsed_url['host'])) { // Not an URL. This should never happen. return false; } //Do not search for a pingback server on our own uploads $uploads_dir = wp_get_upload_dir(); if (0 === strpos($url, $uploads_dir['baseurl'])) { return false; } $response = wp_safe_remote_head($url, array('timeout' => 2, 'httpversion' => '1.0')); if (is_wp_error($response)) { return false; } if (wp_remote_retrieve_header($response, 'x-pingback')) { return wp_remote_retrieve_header($response, 'x-pingback'); } // Not an (x)html, sgml, or xml page, no use going further. if (preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header($response, 'content-type'))) { return false; } // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file) $response = wp_safe_remote_get($url, array('timeout' => 2, 'httpversion' => '1.0')); if (is_wp_error($response)) { return false; } $contents = wp_remote_retrieve_body($response); $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote); $pingback_link_offset_squote = strpos($contents, $pingback_str_squote); if ($pingback_link_offset_dquote || $pingback_link_offset_squote) { $quote = $pingback_link_offset_dquote ? '"' : '\''; $pingback_link_offset = $quote == '"' ? $pingback_link_offset_dquote : $pingback_link_offset_squote; $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset); $pingback_href_start = $pingback_href_pos + 6; $pingback_href_end = @strpos($contents, $quote, $pingback_href_start); $pingback_server_url_len = $pingback_href_end - $pingback_href_start; $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len); // We may find rel="pingback" but an incomplete pingback URL if ($pingback_server_url_len > 0) { // We got it! return $pingback_server_url; } } return false; }
/** * Initialize the widget by setting localization, filters, and administration functions. * * @since 1.0.0 */ public function __construct() { // Load plugin text domain add_action('init', array($this, 'widget_textdomain')); // Upgrade check add_action('init', array($this, 'upgrade_check')); // Check location on template redirect add_action('template_redirect', array($this, 'is_single')); // Hook fired when a new blog is activated on WP Multisite add_action('wpmu_new_blog', array($this, 'activate_new_site')); // Notices check add_action('admin_notices', array($this, 'check_admin_notices')); // Create the widget parent::__construct('wpp', 'WordPress Popular Posts', array('classname' => 'popular-posts', 'description' => __('The most Popular Posts on your blog.', $this->plugin_slug))); // Get user options $this->user_settings = get_site_option('wpp_settings_config'); if (!$this->user_settings) { add_site_option('wpp_settings_config', $this->default_user_settings); $this->user_settings = $this->default_user_settings; } else { $this->user_settings = $this->__merge_array_r($this->default_user_settings, $this->user_settings); } // Allow WP themers / coders to override data sampling status (active/inactive) $this->user_settings['tools']['sampling']['active'] = apply_filters('wpp_data_sampling', $this->user_settings['tools']['sampling']['active']); // Add the options page and menu item. add_action('admin_menu', array($this, 'add_plugin_admin_menu')); // Register admin styles and scripts add_action('admin_print_styles', array($this, 'register_admin_styles')); add_action('admin_enqueue_scripts', array($this, 'register_admin_scripts')); add_action('admin_init', array($this, 'thickbox_setup')); // Register site styles and scripts if ($this->user_settings['tools']['css']) { add_action('wp_enqueue_scripts', array($this, 'register_widget_styles')); } add_action('wp_enqueue_scripts', array($this, 'register_widget_scripts')); // Add plugin settings link add_filter('plugin_action_links', array($this, 'add_plugin_settings_link'), 10, 2); // Set plugin directory $this->plugin_dir = plugin_dir_url(__FILE__); // Get blog charset $this->charset = get_bloginfo('charset'); // Add ajax table truncation to wp_ajax_ hook add_action('wp_ajax_wpp_clear_data', array($this, 'clear_data')); // Add thumbnail cache truncation to wp_ajax_ hook add_action('wp_ajax_wpp_clear_thumbnail', array($this, 'clear_thumbnails')); // Add ajax hook for widget add_action('wp_ajax_wpp_get_popular', array($this, 'get_popular')); add_action('wp_ajax_nopriv_wpp_get_popular', array($this, 'get_popular')); // Check if images can be created if (extension_loaded('ImageMagick') || extension_loaded('GD') && function_exists('gd_info')) { // Enable thumbnail feature $this->thumbnailing = true; // Get available thumbnail size(s) $this->default_thumbnail_sizes = $this->__get_image_sizes(); // Add hook to flush cached thumbnail when image is changed add_action('update_postmeta', array($this, 'flush_post_thumbnail'), 10, 4); } // Set default thumbnail $this->default_thumbnail = $this->plugin_dir . "no_thumb.jpg"; $this->default_user_settings['tools']['thumbnail']['default'] = $this->default_thumbnail; if (!empty($this->user_settings['tools']['thumbnail']['default'])) { $this->default_thumbnail = $this->user_settings['tools']['thumbnail']['default']; } else { $this->user_settings['tools']['thumbnail']['default'] = $this->default_thumbnail; } // Set uploads folder $wp_upload_dir = function_exists('wp_get_upload_dir') ? wp_get_upload_dir() : wp_upload_dir(); // wp_get_upload_dir() was introduced in WP 4.5! $this->uploads_dir['basedir'] = $wp_upload_dir['basedir'] . "/" . $this->plugin_slug; $this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl'] . "/" . $this->plugin_slug; if (!is_dir($this->uploads_dir['basedir'])) { if (!wp_mkdir_p($this->uploads_dir['basedir'])) { $this->uploads_dir['basedir'] = $wp_upload_dir['basedir']; $this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl']; } } // qTrans plugin support if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) { $this->qTrans = true; } // Remove post/page prefetching! remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); // Add the update hooks only if the logging conditions are met if (0 == $this->user_settings['tools']['log']['level'] && !is_user_logged_in() || 1 == $this->user_settings['tools']['log']['level'] || 2 == $this->user_settings['tools']['log']['level'] && is_user_logged_in()) { add_action('wp_head', array(&$this, 'print_ajax')); // Register views from everyone and/or connected users if (0 != $this->user_settings['tools']['log']['level']) { add_action('wp_ajax_update_views_ajax', array($this, 'update_views_ajax')); } // Register views from everyone and/or visitors only if (2 != $this->user_settings['tools']['log']['level']) { add_action('wp_ajax_nopriv_update_views_ajax', array($this, 'update_views_ajax')); } } // Add shortcode add_shortcode('wpp', array(&$this, 'shortcode')); // Purge post data from DB on deletion add_action('admin_init', array($this, 'purge_post_init')); // Enable data purging at midnight if (1 == $this->user_settings['tools']['log']['limit']) { add_action('wpp_cache_event', array($this, 'purge_data')); if (!wp_next_scheduled('wpp_cache_event')) { $tomorrow = time() + 86400; $midnight = mktime(0, 0, 0, date("m", $tomorrow), date("d", $tomorrow), date("Y", $tomorrow)); wp_schedule_event($midnight, 'daily', 'wpp_cache_event'); } } else { // Remove the scheduled event if exists if ($timestamp = wp_next_scheduled('wpp_cache_event')) { wp_unschedule_event($timestamp, 'wpp_cache_event'); } } }
/** * Original File path * * @param string $original_file * */ function original_file($original_file = '') { $uploads = wp_get_upload_dir(); $upload_path = $uploads['basedir']; return path_join($upload_path, $original_file); }
/** * Returns the base URL of the uploads directory. * * @since 4.4.0 * @access private * @deprecated 4.5.0 * * @return string The base URL. */ function _wp_upload_dir_baseurl() { $upload_dir = wp_get_upload_dir(); return $upload_dir['baseurl']; }
/** * Gives the WordPress's default attachment URL if the base URL of the attachment is * different than the WordPress's default base URL. e.g following URL * https://s3.amazonaws.com/bucket-name/wp-content/uploads/2016/09/attachment.jpg * will get replaced with * http://www.wordpress-base.url/wp-content/uploads/2016/09/1473432502-small-10-1-16_1.jpg * * @param int $thumbnail_id It can be attachment URL or attachment ID * @param string $media_type Media type * @param int $media_id Attachment ID * * @return string Attachment URL if attachment URL is provided in the argument */ function rtt_restore_og_wp_image_url($thumbnail_id, $media_type, $media_id) { if (is_numeric($thumbnail_id)) { return $thumbnail_id; } /** * Allow users/plugins to prevent replacing of URL of album cover * * @var boolean Boolean false is passed as a parameter. * @var string $media_type Type of the media. */ if (apply_filters('rtt_restore_og_wp_image_url', false, $media_type)) { return $thumbnail_id; } /** * Fix for rtAmazon S3 addon * When rtAmazon S3 is disabled we need to restore/replace the attachment URLS with the * original WordPress URL structure */ if (!class_exists('RTAWSS3_Class') && !class_exists('AS3CF_Utils')) { /* for WordPress backward compatibility */ if (function_exists('wp_get_upload_dir')) { $uploads = wp_get_upload_dir(); } else { $uploads = wp_upload_dir(); } if (0 === strpos($thumbnail_id, $uploads['baseurl'])) { /* URL is clean here */ /* Apply any filter here if its required */ } else { $baseurl = $uploads['baseurl']; $rtmedia_folder_name = apply_filters('rtmedia_upload_folder_name', 'rtMedia'); $thumbnail_url = explode($rtmedia_folder_name, $thumbnail_id); if (is_array($thumbnail_url) && !empty($thumbnail_url[1])) { $thumbnail_url = $baseurl . '/' . $rtmedia_folder_name . '/' . ltrim($thumbnail_url[1], '/'); } else { $thumbnail_url = $thumbnail_id; } } if (!empty($thumbnail_url)) { $thumbnail_id = $thumbnail_url; } } /** * Apply filter to get amazon s3 URL */ $final_file_url = apply_filters('transcoded_file_url', $thumbnail_id, $media_id); return $final_file_url; }
/** * Create the image attachment and return the new media upload id. * * @author Joshua David Nelson, josh@joshuadnelson.com * * @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example * * @link https://joshuadnelson.com/programmatically-add-images-to-media-library/ * * @param string $image_url The url to the image you're adding to the Media library. * @param int $parent_post_id Optional. Use to attach the media file to a specific post. */ function exchange_create_image_id($image_url, $parent_post_id = null) { // Bail if the image url isn't valid. if (empty($image_url) || !esc_url($image_url)) { return false; } // Escape the url, just to be safe. $image_url = esc_url($image_url); // Cache info on the wp uploads dir. $wp_upload_dir = wp_get_upload_dir(); $wp_upload_path = $wp_upload_dir['basedir']; // Get the file path. $path_array = explode('uploads', $image_url); // File base name, e.g. image.jpg. $file_base_name = basename($image_url); // Combine the two to get the uploaded file path. $uploaded_file_path = $wp_upload_path . $path_array[1]; // Check the type of file. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype($file_base_name, null); // Error check. if (!empty($filetype) && is_array($filetype)) { // Create attachment title - basically, pull out the text. $post_title = preg_replace('/\\.[^.]+$/', '', $file_base_name); // Prepare an array of post data for the attachment. $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($uploaded_file_path), 'post_mime_type' => $filetype['type'], 'post_title' => esc_attr($post_title), 'post_content' => '', 'post_status' => 'inherit'); // Set the post parent id if there is one. if (!is_null($parent_post_id) && absint($parent_post_id)) { $attachment['post_parent'] = absint($parent_post_id); } // Insert the attachment. $attach_id = wp_insert_attachment($attachment, $uploaded_file_path); // Error check. if (!is_wp_error($attach_id)) { // Generate wp attachment meta data. if (file_exists(ABSPATH . 'wp-admin/includes/image.php') && file_exists(ABSPATH . 'wp-admin/includes/media.php')) { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $uploaded_file_path); wp_update_attachment_metadata($attach_id, $attach_data); } // End if file exists check. $attachment_acf = acf_get_attachment($attach_id); } return $attach_id; } else { return false; } // End if $filetype. }
private function delete_local_sizes($meta_sizes, $image_sizes, $main_file, $dir_name) { $uploads = wp_get_upload_dir(); $errors = new WP_Error(); foreach ($meta_sizes as $size => $data) { if (!in_array($size, $image_sizes, true)) { continue; } $file = str_replace(basename($main_file), $data['file'], $main_file); $path = path_join($uploads['basedir'], $file); if (!@unlink($path)) { $errors->add('cannot_delete_file', sprintf(__('The file of size %s for attachment %d could not be deleted.', 'wp-gcs-offload'), $size, $this->id)); continue; } } if (empty($errors->errors)) { return true; } return $errors; }
function exchange_image_tagger() { $result = array(); $collab_args = array('post_type' => 'collaboration', 'posts_per_page' => -1, 'post_status' => 'publish'); $query = new WP_Query($collab_args); $collabs = $query->posts; $upload_path = wp_get_upload_dir(); if (!empty($collabs)) { foreach ($collabs as $collab) { if (has_post_thumbnail($collab)) { $parent_id = wp_get_post_parent_id($collab->ID); echo "parent_id: " . $parent_id . "<br />"; if (!empty($parent_id)) { $parent_name = get_the_title($parent_id); echo "parent_name: " . $parent_name . "<br />"; $thumb_id = get_post_thumbnail_id($collab); echo "thumb_id : " . $thumb_id . "<br />"; if ($thumb_id > 0) { echo "trying to attach<hr />"; $term = get_term_by('name', $parent_name, 'media_category'); if (is_object($term) && get_class($term) === 'WP_Term') { $term = wp_set_object_terms($thumb_id, $term->term_id, 'media_category', true); } else { echo "no term object< br />"; } echo $term->name; } } else { echo "no parent<br />"; $fail++; } } else { echo "no thumbnail<br />"; continue; } } echo "<hr>" . $success . " successes and " . $fail . " failures."; } }
/** * Get dir for notifications. */ public static function getNotificationsDir() { $upload_dir = wp_get_upload_dir(); $dir = $upload_dir["basedir"] . "/crypto-accounts-notifications/"; return $dir; }
function sp_barebones_setup($check = true) { # install picks up wrong SF STORE DIR so lets recalculate it for installs if (is_multisite() && !get_site_option('ms_files_rewriting')) { $uploads = wp_get_upload_dir(); if (!defined('STORE_DIR')) { define('STORE_DIR', $uploads['basedir']); } } else { if (!defined('STORE_DIR')) { define('STORE_DIR', WP_CONTENT_DIR); } } # if exists then get right out if ($check) { if (file_exists(STORE_DIR . '/' . 'sp-custom-settings/sp-barebones-custom-settings.php')) { return; } } # create folder of not exists $perms = fileperms(STORE_DIR); if ($perms === false) { $perms = 0755; } if (!file_exists(STORE_DIR . '/' . 'sp-custom-settings')) { @mkdir(STORE_DIR . '/' . 'sp-custom-settings', $perms); } # compile default file contents $C1 = '#000000'; $C2 = '#FFFFFF'; $C3 = '#BFCBC5'; $C4 = '#A5B1AB'; $C5 = '#3D7157'; $C6 = '#F5F5F5'; $C7 = '#000000'; $C8 = '#3D7157'; $C9 = '#000000'; $C10 = '#000000'; $C11 = '#3D7157'; $FN = 'Tahoma'; $F1 = '100'; $ops = "<?php\n"; $ops .= "\$ops = array(\n"; $ops .= "'C1' => '" . $C1 . "',\n"; $ops .= "'C2' => '" . $C2 . "',\n"; $ops .= "'C3' => '" . $C3 . "',\n"; $ops .= "'C4' => '" . $C4 . "',\n"; $ops .= "'C5' => '" . $C5 . "',\n"; $ops .= "'C6' => '" . $C6 . "',\n"; $ops .= "'C7' => '" . $C7 . "',\n"; $ops .= "'C8' => '" . $C8 . "',\n"; $ops .= "'C9' => '" . $C9 . "',\n"; $ops .= "'C10' => '" . $C10 . "',\n"; $ops .= "'C11' => '" . $C11 . "',\n"; $ops .= "'FN' => '" . $FN . "',\n"; $ops .= "'F1' => '" . $F1 . "',\n"; $ops .= ");\n?>"; $files = array(); $files[] = STORE_DIR . '/' . 'sp-custom-settings/sp-barebones-custom-settings.php'; $files[] = STORE_DIR . '/' . 'sp-custom-settings/sp-barebones-test-settings.php'; foreach ($files as $file) { $f = fopen($file, 'w'); if ($f !== false) { fwrite($f, $ops); fclose($f); } } # Not sure of there is any real way to sned message of success or failure. return; }