Exemplo n.º 1
0
/**
 * Enqueues all scripts, styles, settings, and templates necessary to use
 * all media JS APIs.
 *
 * @since 0.0.1
 *
 * @global int       $content_width
 * @global hqdb      $hqdb
 * @global HQ_Locale $hq_locale
 *
 * @param array $args {
 *     Arguments for enqueuing media scripts.
 *
 *     @type int|HQ_Post A post object or ID.
 * }
 */
function hq_enqueue_media($args = array())
{
    // Enqueue me just once per page, please.
    if (did_action('hq_enqueue_media')) {
        return;
    }
    global $content_width, $hqdb, $hq_locale;
    $defaults = array('post' => null);
    $args = hq_parse_args($args, $defaults);
    // We're going to pass the old thickbox media tabs to `media_upload_tabs`
    // to ensure plugins will work. We will then unset those tabs.
    $tabs = array('type' => '', 'type_url' => '', 'gallery' => '', 'library' => '');
    /** This filter is documented in hq-admin/includes/media.php */
    $tabs = apply_filters('media_upload_tabs', $tabs);
    unset($tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library']);
    $props = array('link' => get_option('image_default_link_type'), 'align' => get_option('image_default_align'), 'size' => get_option('image_default_size'));
    $exts = array_merge(hq_get_audio_extensions(), hq_get_video_extensions());
    $mimes = get_allowed_mime_types();
    $ext_mimes = array();
    foreach ($exts as $ext) {
        foreach ($mimes as $ext_preg => $mime_match) {
            if (preg_match('#' . $ext . '#i', $ext_preg)) {
                $ext_mimes[$ext] = $mime_match;
                break;
            }
        }
    }
    $has_audio = $hqdb->get_var("\n\t\tSELECT ID\n\t\tFROM {$hqdb->posts}\n\t\tWHERE post_type = 'attachment'\n\t\tAND post_mime_type LIKE 'audio%'\n\t\tLIMIT 1\n\t");
    $has_video = $hqdb->get_var("\n\t\tSELECT ID\n\t\tFROM {$hqdb->posts}\n\t\tWHERE post_type = 'attachment'\n\t\tAND post_mime_type LIKE 'video%'\n\t\tLIMIT 1\n\t");
    $months = $hqdb->get_results($hqdb->prepare("\n\t\tSELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month\n\t\tFROM {$hqdb->posts}\n\t\tWHERE post_type = %s\n\t\tORDER BY post_date DESC\n\t", 'attachment'));
    foreach ($months as $month_year) {
        $month_year->text = sprintf(__('%1$s %2$d'), $hq_locale->get_month($month_year->month), $month_year->year);
    }
    $settings = array('tabs' => $tabs, 'tabUrl' => add_query_arg(array('chromeless' => true), admin_url('media-upload.php')), 'mimeTypes' => hq_list_pluck(get_post_mime_types(), 0), 'captions' => !apply_filters('disable_captions', ''), 'nonce' => array('sendToEditor' => hq_create_nonce('media-send-to-editor')), 'post' => array('id' => 0), 'defaultProps' => $props, 'attachmentCounts' => array('audio' => $has_audio ? 1 : 0, 'video' => $has_video ? 1 : 0), 'embedExts' => $exts, 'embedMimes' => $ext_mimes, 'contentWidth' => $content_width, 'months' => $months, 'mediaTrash' => MEDIA_TRASH ? 1 : 0);
    $post = null;
    if (isset($args['post'])) {
        $post = get_post($args['post']);
        $settings['post'] = array('id' => $post->ID, 'nonce' => hq_create_nonce('update-post_' . $post->ID));
        $thumbnail_support = current_theme_supports('post-thumbnails', $post->post_type) && post_type_supports($post->post_type, 'thumbnail');
        if (!$thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type) {
            if (hq_attachment_is('audio', $post)) {
                $thumbnail_support = post_type_supports('attachment:audio', 'thumbnail') || current_theme_supports('post-thumbnails', 'attachment:audio');
            } elseif (hq_attachment_is('video', $post)) {
                $thumbnail_support = post_type_supports('attachment:video', 'thumbnail') || current_theme_supports('post-thumbnails', 'attachment:video');
            }
        }
        if ($thumbnail_support) {
            $featured_image_id = get_post_meta($post->ID, '_thumbnail_id', true);
            $settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1;
        }
    }
    $hier = $post && is_post_type_hierarchical($post->post_type);
    if ($post) {
        $post_type_object = get_post_type_object($post->post_type);
    } else {
        $post_type_object = get_post_type_object('post');
    }
    $strings = array('url' => __('URL'), 'addMedia' => __('Add Media'), 'search' => __('Search'), 'select' => __('Select'), 'cancel' => __('Cancel'), 'update' => __('Update'), 'replace' => __('Replace'), 'remove' => __('Remove'), 'back' => __('Back'), 'selected' => __('%d selected'), 'dragInfo' => __('Drag and drop to reorder media files.'), 'uploadFilesTitle' => __('Upload Files'), 'uploadImagesTitle' => __('Upload Images'), 'mediaLibraryTitle' => __('Media Library'), 'insertMediaTitle' => __('Insert Media'), 'createNewGallery' => __('Create a new gallery'), 'createNewPlaylist' => __('Create a new playlist'), 'createNewVideoPlaylist' => __('Create a new video playlist'), 'returnToLibrary' => __('← Return to library'), 'allMediaItems' => __('All media items'), 'allDates' => __('All dates'), 'noItemsFound' => __('No items found.'), 'insertIntoPost' => $hier ? __('Insert into page') : __('Insert into post'), 'unattached' => __('Unattached'), 'trash' => _x('Trash', 'noun'), 'uploadedToThisPost' => $hier ? __('Uploaded to this page') : __('Uploaded to this post'), 'warnDelete' => __("You are about to permanently delete this item.\n  'Cancel' to stop, 'OK' to delete."), 'warnBulkDelete' => __("You are about to permanently delete these items.\n  'Cancel' to stop, 'OK' to delete."), 'warnBulkTrash' => __("You are about to trash these items.\n  'Cancel' to stop, 'OK' to delete."), 'bulkSelect' => __('Bulk Select'), 'cancelSelection' => __('Cancel Selection'), 'trashSelected' => __('Trash Selected'), 'untrashSelected' => __('Untrash Selected'), 'deleteSelected' => __('Delete Selected'), 'deletePermanently' => __('Delete Permanently'), 'apply' => __('Apply'), 'filterByDate' => __('Filter by date'), 'filterByType' => __('Filter by type'), 'searchMediaLabel' => __('Search Media'), 'noMedia' => __('No media attachments found.'), 'attachmentDetails' => __('Attachment Details'), 'insertFromUrlTitle' => __('Insert from URL'), 'setFeaturedImageTitle' => $post_type_object->labels->featured_image, 'setFeaturedImage' => $post_type_object->labels->set_featured_image, 'createGalleryTitle' => __('Create Gallery'), 'editGalleryTitle' => __('Edit Gallery'), 'cancelGalleryTitle' => __('← Cancel Gallery'), 'insertGallery' => __('Insert gallery'), 'updateGallery' => __('Update gallery'), 'addToGallery' => __('Add to gallery'), 'addToGalleryTitle' => __('Add to Gallery'), 'reverseOrder' => __('Reverse order'), 'imageDetailsTitle' => __('Image Details'), 'imageReplaceTitle' => __('Replace Image'), 'imageDetailsCancel' => __('Cancel Edit'), 'editImage' => __('Edit Image'), 'chooseImage' => __('Choose Image'), 'selectAndCrop' => __('Select and Crop'), 'skipCropping' => __('Skip Cropping'), 'cropImage' => __('Crop Image'), 'cropYourImage' => __('Crop your image'), 'cropping' => __('Cropping…'), 'suggestedDimensions' => __('Suggested image dimensions:'), 'cropError' => __('There has been an error cropping your image.'), 'audioDetailsTitle' => __('Audio Details'), 'audioReplaceTitle' => __('Replace Audio'), 'audioAddSourceTitle' => __('Add Audio Source'), 'audioDetailsCancel' => __('Cancel Edit'), 'videoDetailsTitle' => __('Video Details'), 'videoReplaceTitle' => __('Replace Video'), 'videoAddSourceTitle' => __('Add Video Source'), 'videoDetailsCancel' => __('Cancel Edit'), 'videoSelectPosterImageTitle' => __('Select Poster Image'), 'videoAddTrackTitle' => __('Add Subtitles'), 'playlistDragInfo' => __('Drag and drop to reorder tracks.'), 'createPlaylistTitle' => __('Create Audio Playlist'), 'editPlaylistTitle' => __('Edit Audio Playlist'), 'cancelPlaylistTitle' => __('← Cancel Audio Playlist'), 'insertPlaylist' => __('Insert audio playlist'), 'updatePlaylist' => __('Update audio playlist'), 'addToPlaylist' => __('Add to audio playlist'), 'addToPlaylistTitle' => __('Add to Audio Playlist'), 'videoPlaylistDragInfo' => __('Drag and drop to reorder videos.'), 'createVideoPlaylistTitle' => __('Create Video Playlist'), 'editVideoPlaylistTitle' => __('Edit Video Playlist'), 'cancelVideoPlaylistTitle' => __('← Cancel Video Playlist'), 'insertVideoPlaylist' => __('Insert video playlist'), 'updateVideoPlaylist' => __('Update video playlist'), 'addToVideoPlaylist' => __('Add to video playlist'), 'addToVideoPlaylistTitle' => __('Add to Video Playlist'));
    /**
     * Filter the media view settings.
     *
     * @since 0.0.1
     *
     * @param array   $settings List of media view settings.
     * @param HQ_Post $post     Post object.
     */
    $settings = apply_filters('media_view_settings', $settings, $post);
    /**
     * Filter the media view strings.
     *
     * @since 0.0.1
     *
     * @param array   $strings List of media view strings.
     * @param HQ_Post $post    Post object.
     */
    $strings = apply_filters('media_view_strings', $strings, $post);
    $strings['settings'] = $settings;
    // Ensure we enqueue media-editor first, that way media-views is
    // registered internally before we try to localize it. see #24724.
    hq_enqueue_script('media-editor');
    hq_localize_script('media-views', '_hqMediaViewsL10n', $strings);
    hq_enqueue_script('media-audiovideo');
    hq_enqueue_style('media-views');
    if (is_admin()) {
        hq_enqueue_script('mce-view');
        hq_enqueue_script('image-edit');
    }
    hq_enqueue_style('imgareaselect');
    hq_plupload_default_settings();
    require_once ABSPATH . HQINC . '/media-template.php';
    add_action('admin_footer', 'hq_print_media_templates');
    add_action('hq_footer', 'hq_print_media_templates');
    add_action('customize_controls_print_footer_scripts', 'hq_print_media_templates');
    /**
     * Fires at the conclusion of hq_enqueue_media().
     *
     * @since 0.0.1
     */
    do_action('hq_enqueue_media');
}
Exemplo n.º 2
0
/**
 * Populate network settings.
 *
 * @since 0.0.1
 *
 * @global hqdb       $hqdb
 * @global object     $current_site
 * @global int        $hq_db_version
 * @global HQ_Rewrite $hq_rewrite
 *
 * @param int $network_id ID of network to populate.
 * @return bool|HQ_Error True on success, or HQ_Error on warning (with the install otherwise successful,
 *                       so the error code must be checked) or failure.
 */
function populate_network($network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false)
{
    global $hqdb, $current_site, $hq_db_version, $hq_rewrite;
    $errors = new HQ_Error();
    if ('' == $domain) {
        $errors->add('empty_domain', __('You must provide a domain name.'));
    }
    if ('' == $site_name) {
        $errors->add('empty_sitename', __('You must provide a name for your network of sites.'));
    }
    // Check for network collision.
    if ($network_id == $hqdb->get_var($hqdb->prepare("SELECT id FROM {$hqdb->site} WHERE id = %d", $network_id))) {
        $errors->add('siteid_exists', __('The network already exists.'));
    }
    $site_user = get_user_by('email', $email);
    if (!is_email($email)) {
        $errors->add('invalid_email', __('You must provide a valid e-mail address.'));
    }
    if ($errors->get_error_code()) {
        return $errors;
    }
    // Set up site tables.
    $template = get_option('template');
    $stylesheet = get_option('stylesheet');
    $allowed_themes = array($stylesheet => true);
    if ($template != $stylesheet) {
        $allowed_themes[$template] = true;
    }
    if (HQ_DEFAULT_THEME != $stylesheet && HQ_DEFAULT_THEME != $template) {
        $allowed_themes[HQ_DEFAULT_THEME] = true;
    }
    if (1 == $network_id) {
        $hqdb->insert($hqdb->site, array('domain' => $domain, 'path' => $path));
        $network_id = $hqdb->insert_id;
    } else {
        $hqdb->insert($hqdb->site, array('domain' => $domain, 'path' => $path, 'id' => $network_id));
    }
    hq_cache_delete('networks_have_paths', 'site-options');
    //TODO: no multisite
    //if ( !is_multisite() ) {
    $site_admins = array($site_user->user_login);
    $users = get_users(array('fields' => array('ID', 'user_login')));
    if ($users) {
        foreach ($users as $user) {
            if (is_super_admin($user->ID) && !in_array($user->user_login, $site_admins)) {
                $site_admins[] = $user->user_login;
            }
        }
    }
    //} else {
    //	$site_admins = get_site_option( 'site_admins' );
    //}
    /* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
    $welcome_email = __('Howdy USERNAME,

Your new SITE_NAME site has been successfully set up at:
BLOG_URL

You can log in to the administrator account with the following information:

Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLhq-login.php

We hope you enjoy your new site. Thanks!

--The Team @ SITE_NAME');
    $misc_exts = array('jpg', 'jpeg', 'png', 'gif', 'mov', 'avi', 'mpg', '3gp', '3g2', 'midi', 'mid', 'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key');
    $audio_exts = hq_get_audio_extensions();
    $video_exts = hq_get_video_extensions();
    $upload_filetypes = array_unique(array_merge($misc_exts, $audio_exts, $video_exts));
    $sitemeta = array('site_name' => $site_name, 'admin_email' => $site_user->user_email, 'admin_user_id' => $site_user->ID, 'registration' => 'none', 'upload_filetypes' => implode(' ', $upload_filetypes), 'blog_upload_space' => 100, 'fileupload_maxk' => 1500, 'site_admins' => $site_admins, 'allowedthemes' => $allowed_themes, 'illegal_names' => array('www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files'), 'hqmu_upgrade_site' => $hq_db_version, 'welcome_email' => $welcome_email, 'first_post' => __('Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!'), 'siteurl' => get_option('siteurl') . '/', 'add_new_users' => '0', 'upload_space_check_disabled' => '1', 'subdomain_install' => intval($subdomain_install), 'global_terms_enabled' => global_terms_enabled() ? '1' : '0', 'ms_files_rewriting' => '0', 'initial_db_version' => get_option('initial_db_version'), 'active_sitewide_plugins' => array(), 'HQLANG' => get_locale());
    if (!$subdomain_install) {
        $sitemeta['illegal_names'][] = 'blog';
    }
    /**
     * Filter meta for a network on creation.
     *
     * @since 0.0.1
     *
     * @param array $sitemeta   Associative array of network meta keys and values to be inserted.
     * @param int   $network_id ID of network to populate.
     */
    $sitemeta = apply_filters('populate_network_meta', $sitemeta, $network_id);
    $insert = '';
    foreach ($sitemeta as $meta_key => $meta_value) {
        if (is_array($meta_value)) {
            $meta_value = serialize($meta_value);
        }
        if (!empty($insert)) {
            $insert .= ', ';
        }
        $insert .= $hqdb->prepare("( %d, %s, %s)", $network_id, $meta_key, $meta_value);
    }
    $hqdb->query("INSERT INTO {$hqdb->sitemeta} ( site_id, meta_key, meta_value ) VALUES " . $insert);
    /*
     * When upgrading from single to multisite, assume the current site will
     * become the main site of the network. When using populate_network()
     * to create another network in an existing multisite environment, skip
     * these steps since the main site of the new network has not yet been
     * created.
     */
    //TODO: no multisite
    //if ( ! is_multisite() ) {
    $current_site = new stdClass();
    $current_site->domain = $domain;
    $current_site->path = $path;
    $current_site->site_name = ucfirst($domain);
    $hqdb->insert($hqdb->blogs, array('site_id' => $network_id, 'blog_id' => 1, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')));
    $current_site->blog_id = $blog_id = $hqdb->insert_id;
    update_user_meta($site_user->ID, 'source_domain', $domain);
    update_user_meta($site_user->ID, 'primary_blog', $blog_id);
    if ($subdomain_install) {
        $hq_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
    } else {
        $hq_rewrite->set_permalink_structure('/blog/%year%/%monthnum%/%day%/%postname%/');
    }
    flush_rewrite_rules();
    if (!$subdomain_install) {
        return true;
    }
    $vhost_ok = false;
    $errstr = '';
    $hostname = substr(md5(time()), 0, 6) . '.' . $domain;
    // Very random hostname!
    $page = hq_remote_get('http://' . $hostname, array('timeout' => 5, 'httpversion' => '1.1'));
    if (is_hq_error($page)) {
        $errstr = $page->get_error_message();
    } elseif (200 == hq_remote_retrieve_response_code($page)) {
        $vhost_ok = true;
    }
    if (!$vhost_ok) {
        $msg = '<p><strong>' . __('Warning! Wildcard DNS may not be configured correctly!') . '</strong></p>';
        $msg .= '<p>' . sprintf(__('The installer attempted to contact a random hostname (<code>%1$s</code>) on your domain.'), $hostname);
        if (!empty($errstr)) {
            $msg .= ' ' . sprintf(__('This resulted in an error message: %s'), '<code>' . $errstr . '</code>');
        }
        $msg .= '</p>';
        $msg .= '<p>' . __('To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a <code>*</code> hostname record pointing at your web server in your DNS configuration tool.') . '</p>';
        $msg .= '<p>' . __('You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.') . '</p>';
        return new HQ_Error('no_wildcard_dns', $msg);
    }
    //}
    return true;
}
Exemplo n.º 3
0
/**
 * Verifies an attachment is of a given type.
 *
 * @since 0.0.1
 *
 * @param string      $type    Attachment type. Accepts 'image', 'audio', or 'video'.
 * @param int|HQ_Post $post_id Optional. Attachment ID. Default 0.
 * @return bool True if one of the accepted types, false otherwise.
 */
function hq_attachment_is($type, $post_id = 0)
{
    if (!($post = get_post($post_id))) {
        return false;
    }
    if (!($file = get_attached_file($post->ID))) {
        return false;
    }
    if (0 === strpos($post->post_mime_type, $type . '/')) {
        return true;
    }
    $check = hq_check_filetype($file);
    if (empty($check['ext'])) {
        return false;
    }
    $ext = $check['ext'];
    if ('import' !== $post->post_mime_type) {
        return $type === $ext;
    }
    switch ($type) {
        case 'image':
            $image_exts = array('jpg', 'jpeg', 'jpe', 'gif', 'png');
            return in_array($ext, $image_exts);
        case 'audio':
            return in_array($ext, hq_get_audio_extensions());
        case 'video':
            return in_array($ext, hq_get_video_extensions());
        default:
            return $type === $ext;
    }
}