Example #1
0
function wpsw_insert_asset($src, $force = false)
{
    if (!$force and $id = wpsw_get_alias_id($src)) {
        return $id;
    }
    // gives us access to the download_url() and wp_handle_sideload() functions
    if (!function_exists('media_handle_upload')) {
        require_once ABSPATH . "wp-admin" . '/includes/image.php';
        require_once ABSPATH . "wp-admin" . '/includes/file.php';
        require_once ABSPATH . "wp-admin" . '/includes/media.php';
    }
    // URL to the WordPress logo
    //$url = get_template_directory_uri() . '/' . $src;
    //$tmp = download_url( $url );
    $tmp = null;
    if (wpsw_is_url($src)) {
        $tmp = download_url($src, 5000);
    } else {
        $url = wpsw_get_path(wpsw_get_config('migrations.markdown_path', 'web/src') . '/' . $src);
        $tmp = wp_tempnam($url);
        @copy($url, $tmp);
    }
    // clearing the stat cache
    clearstatcache(true, $tmp);
    $file_array = array('name' => basename($src), 'tmp_name' => $tmp);
    /**
     * Check for download errors
     * if there are error unlink the temp file name
     */
    if (is_wp_error($tmp)) {
        //@unlink( $file_array[ 'tmp_name' ] );
        return $tmp;
    }
    /**
     * now we can actually use media_handle_sideload
     * we pass it the file array of the file to handle
     * and the post id of the post to attach it to
     * $post_id can be set to '0' to not attach it to any particular post
     */
    $post_id = 0;
    $id = media_handle_sideload($file_array, $post_id);
    /**
     * We don't want to pass something to $id
     * if there were upload errors.
     * So this checks for errors
     */
    if (is_wp_error($id)) {
        @unlink($file_array['tmp_name']);
        return $id;
    }
    add_post_meta($id, '_wpsw_alias', $src, true);
    @unlink($file_array['tmp_name']);
    return $id;
}
Example #2
0
 protected function create_nav_menu()
 {
     if (!function_exists('wp_create_nav_menu')) {
         require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     }
     $nav_menu_locations = array();
     foreach (wpsw_get_config('register.nav_menus') as $menu_key => $menu_name) {
         wp_delete_nav_menu($menu_name);
         $items = array_get($this->menus, $menu_key, array());
         $menu_items = array();
         foreach ($items as $key => $item) {
             $data = array();
             $data['menu-item-title'] = $item['title'];
             $data['menu-item-position'] = $key + 1;
             if (isset($item['class'])) {
                 $data['menu-item-classes'] = explode(' ', $item['class']);
             }
             if (isset($item['link'])) {
                 $src = preg_replace('/\\.html$/i', '.md', $item['link']);
                 $id = wpsw_get_alias_id($src);
                 $post = get_post($id);
                 if ($post) {
                     $data['menu-item-type'] = 'post_type';
                     $data['menu-item-object'] = $post->post_type;
                     $data['menu-item-object-id'] = $id;
                 } else {
                     $data['menu-item-url'] = $item['link'];
                 }
             }
             $menu_item = $this->perpare_nav_item_data($data);
             $menu_item_db_id = $this->add_nav_menu_item($menu_name, $menu_item);
             pc($menu_item_db_id, 'menu_item_db_id');
             foreach (array_get($item, 'sub_menu', array()) as $index => $sub_item) {
                 $data = array();
                 $data['menu-item-title'] = $sub_item['title'];
                 $data['menu-item-position'] = $index + 1;
                 $data['menu-item-parent-id'] = $menu_item_db_id;
                 if (isset($sub_item['class'])) {
                     $data['menu-item-classes'] = explode(' ', $sub_item['class']);
                 }
                 if (isset($sub_item['link'])) {
                     $src = preg_replace('/\\.html$/i', '.md', $sub_item['link']);
                     $id = wpsw_get_alias_id($src);
                     $post = get_post($id);
                     if ($post) {
                         $data['menu-item-type'] = 'post_type';
                         $data['menu-item-object'] = $post->post_type;
                         $data['menu-item-object-id'] = $id;
                     } else {
                         $data['menu-item-url'] = $sub_item['link'];
                     }
                 }
                 $sub_menu_item = $this->perpare_nav_item_data($data);
                 $sub_menu_item_db_id = $this->add_nav_menu_item($menu_name, $sub_menu_item);
             }
         }
         // items loop
         $menu = wp_get_nav_menu_object($menu_name);
         $nav_menu_locations[$menu_key] = $menu->term_id;
     }
     // register.nav_menus loop
     $theme_mods = array(false, 'nav_menu_locations' => $nav_menu_locations);
     $theme = get_option('stylesheet');
     if (get_option("theme_mods_{$theme}")) {
         update_option("theme_mods_{$theme}", $theme_mods);
     } else {
         add_option("theme_mods_{$theme}", $theme_mods);
     }
 }