예제 #1
0
/**
 * Inits custom types.
 */
function wpcf_custom_types_init()
{
    $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    if (!empty($custom_types)) {
        foreach ($custom_types as $post_type => $data) {
            if (empty($data)) {
                continue;
            }
            if (isset($data['_builtin']) && $data['_builtin'] || wpcf_is_builtin_post_types($post_type)) {
                continue;
            }
            wpcf_custom_types_register($post_type, $data);
        }
    }
}
/**
 * Inits custom types.
 */
function wpcf_custom_types_init()
{
    $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    if (!empty($custom_types)) {
        foreach ($custom_types as $post_type => $data) {
            if (empty($data)) {
                continue;
            }
            if (isset($data['_builtin']) && $data['_builtin'] || wpcf_is_builtin_post_types($post_type)) {
                continue;
            }
            wpcf_custom_types_register($post_type, $data);
        }
    }
    // rearrange menu items
    add_filter('custom_menu_order', '__return_true');
    add_filter('menu_order', 'wpcf_custom_types_menu_order_set');
    // rearrange menu items - end
    /** This filter is documented in wp-admin/wp-admin/edit-form-advanced.php */
    add_filter('enter_title_here', 'wpcf_filter_enter_title_here', 10, 2);
}
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 private function save()
 {
     global $wpcf;
     if (!isset($_POST['ct'])) {
         return false;
     }
     $data = $_POST['ct'];
     $update = false;
     // Sanitize data
     if (isset($data[$this->get_id])) {
         $update = true;
         $data[$this->get_id] = sanitize_title($data[$this->get_id]);
     } else {
         $data[$this->get_id] = null;
     }
     if (isset($data['slug'])) {
         $data['slug'] = sanitize_title($data['slug']);
     } elseif ($_GET['wpcf-post-type'] == 'post' || $_GET['wpcf-post-type'] == 'page' || $_GET['wpcf-post-type'] == 'attachment') {
         $data['slug'] = $_GET['wpcf-post-type'];
     } else {
         $data['slug'] = null;
     }
     if (isset($data['rewrite']['slug'])) {
         $data['rewrite']['slug'] = remove_accents($data['rewrite']['slug']);
         $data['rewrite']['slug'] = strtolower($data['rewrite']['slug']);
         $data['rewrite']['slug'] = trim($data['rewrite']['slug']);
     }
     $data['_builtin'] = false;
     // Set post type name
     $post_type = null;
     if (!empty($data['slug'])) {
         $post_type = $data['slug'];
     } elseif (!empty($data[$this->get_id])) {
         $post_type = $data[$this->get_id];
     } elseif (!empty($data['labels']['singular_name'])) {
         $post_type = sanitize_title($data['labels']['singular_name']);
     }
     if (empty($post_type)) {
         wpcf_admin_message(__('Please set post type name', 'wpcf'), 'error');
         return false;
     }
     $data['slug'] = $post_type;
     $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     $protected_data_check = array();
     if (wpcf_is_builtin_post_types($data['slug'])) {
         $data['_builtin'] = true;
         $update = true;
     } else {
         // Check reserved name
         $reserved = wpcf_is_reserved_name($post_type, 'post_type');
         if (is_wp_error($reserved)) {
             wpcf_admin_message($reserved->get_error_message(), 'error');
             return false;
         }
         // Check overwriting
         if ((!array_key_exists($this->get_id, $data) || $data[$this->get_id] != $post_type) && array_key_exists($post_type, $custom_types)) {
             wpcf_admin_message(__('Post Type already exists', 'wpcf'), 'error');
             return false;
         }
         /*
          * Since Types 1.2
          * We do not allow plural and singular names to be same.
          */
         if ($wpcf->post_types->check_singular_plural_match($data)) {
             wpcf_admin_message($wpcf->post_types->message('warning_singular_plural_match'), 'error');
             return false;
         }
         // Check if renaming then rename all post entries and delete old type
         if (!empty($data[$this->get_id]) && $data[$this->get_id] != $post_type) {
             global $wpdb;
             $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('post_type' => $data[$this->get_id]), array('%s'), array('%s'));
             /**
              * update post meta "_wp_types_group_post_types"
              */
             $sql = $wpdb->prepare(sprintf('select meta_id, meta_value from %s where meta_key = %%s', $wpdb->postmeta), '_wp_types_group_post_types');
             $all_meta = $wpdb->get_results($sql, OBJECT_K);
             $re = sprintf('/,%s,/', $data[$this->get_id]);
             foreach ($all_meta as $meta) {
                 if (!preg_match($re, $meta->meta_value)) {
                     continue;
                 }
                 $wpdb->update($wpdb->postmeta, array('meta_value' => preg_replace($re, ',' . $post_type . ',', $meta->meta_value)), array('meta_id' => $meta->meta_id), array('%s'), array('%d'));
             }
             /**
              * update _wpcf_belongs_{$data[$this->get_id]}_id
              */
             $wpdb->update($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $post_type)), array('meta_key' => sprintf('_wpcf_belongs_%s_id', $data[$this->get_id])), array('%s'), array('%s'));
             /**
              * update options "wpv_options"
              */
             $wpv_options = get_option('wpv_options', true);
             if (is_array($wpv_options)) {
                 $re = sprintf('/(views_template_(archive_)?for_)%s/', $data[$this->get_id]);
                 foreach ($wpv_options as $key => $value) {
                     if (!preg_match($re, $key)) {
                         continue;
                     }
                     unset($wpv_options[$key]);
                     $key = preg_replace($re, "\$1" . $post_type, $key);
                     $wpv_options[$key] = $value;
                 }
                 update_option('wpv_options', $wpv_options);
             }
             /**
              * update option "wpcf-custom-taxonomies"
              */
             $wpcf_custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, true);
             if (is_array($wpcf_custom_taxonomies)) {
                 $update_wpcf_custom_taxonomies = false;
                 foreach ($wpcf_custom_taxonomies as $key => $value) {
                     if (array_key_exists('supports', $value) && array_key_exists($data[$this->get_id], $value['supports'])) {
                         unset($wpcf_custom_taxonomies[$key]['supports'][$data[$this->get_id]]);
                         $update_wpcf_custom_taxonomies = true;
                     }
                 }
                 if ($update_wpcf_custom_taxonomies) {
                     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $wpcf_custom_taxonomies);
                 }
             }
             // Sync action
             do_action('wpcf_post_type_renamed', $post_type, $data[$this->get_id]);
             // Set protected data
             $protected_data_check = $custom_types[$data[$this->get_id]];
             // Delete old type
             unset($custom_types[$data[$this->get_id]]);
             $data[$this->get_id] = $post_type;
         } else {
             // Set protected data
             $protected_data_check = !empty($custom_types[$post_type]) ? $custom_types[$post_type] : array();
         }
         // Check if active
         if (isset($custom_types[$post_type]['disabled'])) {
             $data['disabled'] = $custom_types[$post_type]['disabled'];
         }
     }
     // Sync taxes with custom taxes
     $taxes = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array());
     foreach ($taxes as $id => $tax) {
         if (isset($data['taxonomies']) && !empty($data['taxonomies']) && array_key_exists($id, $data['taxonomies'])) {
             $taxes[$id]['supports'][$data['slug']] = 1;
         } else {
             if (isset($taxes[$id]['supports'][$data['slug']])) {
                 unset($taxes[$id]['supports'][$data['slug']]);
             }
         }
     }
     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxes);
     // Preserve protected data
     foreach ($protected_data_check as $key => $value) {
         if (strpos($key, '_') !== 0) {
             unset($protected_data_check[$key]);
         }
     }
     /**
      * save custom field group
      */
     /* removed types-608
        $post_to_groups = isset($_POST['ct']['custom-field-group'])?$_POST['ct']['custom-field-group']:array();
        $groups = $this->fields->get_groups_with_post_types();
        foreach( $groups as $group) {
            $post_types_to_save = $group['_wp_types_group_post_types'];
            // save
            if ( array_key_exists($group['id'], $post_to_groups)) {
                $post_types_to_save[] = $data['slug'];
            } else {
                if(($key = array_search($data['slug'], $post_types_to_save)) !== false) {
                    unset($post_types_to_save[$key]);
                }
                if (
                    false
                    || empty($post_types_to_save)
                    || (
                        true
                        && 1 == sizeof($post_types_to_save)
                        && 'all' == current($post_types_to_save)
                    )
                ) {
                    $post_types_to_save = array();
                    foreach( get_post_types() as $key => $value ) {
                        if ( $data['slug'] == $value) {
                            continue;
                        }
                        if ( in_array($value, $wpcf->excluded_post_types) ) {
                            continue;
                        }
                        $post_types_to_save[] = $value;
                    }
                }
            }
            wpcf_admin_fields_save_group_post_types($group['id'], $post_types_to_save);
        }
        */
     /**
      * set last edit time
      */
     $data[TOOLSET_EDIT_LAST] = time();
     /**
      * set last edit author
      */
     $data[WPCF_AUTHOR] = get_current_user_id();
     /**
      * add builid in
      */
     if ($data['_builtin'] && !isset($protected_data_check[$data['slug']])) {
         $protected_data_check[$data['slug']] = array();
     }
     // Merging protected data
     $custom_types[$post_type] = array_merge($protected_data_check, $data);
     update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types);
     // WPML register strings
     if (!$data['_builtin']) {
         wpcf_custom_types_register_translation($post_type, $data);
     }
     // success message
     $msg = $update ? __('Post Type saved.', 'wpcf') : __('New Post Type created.', 'wpcf');
     wpcf_admin_message_store($msg, 'updated notice notice-success is-dismissible');
     flush_rewrite_rules();
     if (!$data['_builtin']) {
         do_action('wpcf_custom_types_save', $data);
     }
     // Redirect
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-edit-type', $this->get_id => $post_type, 'wpcf-message' => 'view', 'flush' => '1'), admin_url('admin.php'))));
     die;
 }
 /** ************************************************************************
  * Recommended. This is a custom column method and is responsible for what
  * is rendered in any column with a name/slug of 'title'. Every time the class
  * needs to render a column, it first looks for a method named
  * column_{$column_title} - if it exists, that method is run. If it doesn't
  * exist, column_default() is called instead.
  *
  * This example also illustrates how to implement rollover actions. Actions
  * should be an associative array formatted as 'slug'=>'link html' - and you
  * will need to generate the URLs yourself. You could even ensure the links
  *
  *
  * @see WP_List_Table::::single_row_columns()
  * @param array $item A singular item (one full row's worth of data)
  * @return string Text to be placed inside the column <td> (movie title only)
  **************************************************************************/
 function column_title($item)
 {
     $edit_link = esc_url(add_query_arg(array('page' => 'wpcf-view-type', 'wpcf-post-type' => $item['slug']), admin_url('admin.php')));
     if (WPCF_Roles::user_can_edit('custom-post-type', $item)) {
         $edit_link = esc_url(add_query_arg(array('page' => 'wpcf-edit-type', 'wpcf-post-type' => $item['slug']), admin_url('admin.php')));
     }
     //Build row actions
     $actions = array();
     $actions['edit'] = sprintf('<a href="%s">%s</a>', $edit_link, __('Edit', 'wpcf'));
     if ('cpt' == $item['type']) {
         $a = array('status' => 'active' == $item['status'] ? wpcf_admin_custom_types_get_ajax_deactivation_link($item['slug']) : wpcf_admin_custom_types_get_ajax_activation_link($item['slug']), 'duplicate' => sprintf('<a href="%s" class="submitduplicate wpcf-ajax-link" id="wpcf-list-duplicate-%s">%s</a>', esc_url(add_query_arg(array('action' => 'wpcf_ajax', 'wpcf_action' => 'duplicate_post_type', 'wpcf-post-type' => $item['slug'], 'wpcf_ajax_update' => 'wpcf_list_ajax_response_' . $item['slug'], '_wpnonce' => wp_create_nonce('duplicate_post_type')), admin_url('admin-ajax.php'))), $item['slug'], __('Duplicate', 'wpcf')), 'delete' => sprintf('<a href="%s" class="submitdelete wpcf-ajax-link" id="wpcf-list-delete-%s">%s</a>', esc_url(add_query_arg(array('action' => 'wpcf_ajax', 'wpcf_action' => 'delete_post_type', 'wpcf-post-type' => $item['slug'], 'wpcf_ajax_update' => 'wpcf_list_ajax_response_' . $item['slug'], '_wpnonce' => wp_create_nonce('delete_post_type'), 'wpcf_warning' => urlencode(__('Are you sure?', 'wpcf'))), admin_url('admin-ajax.php'))), $item['slug'], __('Delete', 'wpcf')));
         $actions += $a;
     } elseif (!wpcf_is_builtin_post_types($item['slug'])) {
         $actions = array('view' => sprintf('<a href="%s">%s</a>', $edit_link, __('View', 'wpcf')));
     }
     //Return the title contents
     return sprintf('<strong><a href="%s" class="row-title">%s</strong>%s', $edit_link, $item['title'], $this->row_actions($actions));
 }
예제 #5
0
파일: admin.php 프로젝트: zoran180/wp_szf
/**
 * Menu page display.
 */
function wpcf_admin_menu_edit_type()
{
    $title = __('View Custom Post Type', 'wpcf');
    if (WPCF_Roles::user_can_edit('custom-post-type', array())) {
        if (isset($_GET['wpcf-post-type'])) {
            $title = __('Edit Custom Post Type', 'wpcf');
            if (wpcf_is_builtin_post_types($_GET['wpcf-post-type'])) {
                $title = __('Edit Post Type', 'wpcf');
            }
            /**
             * add new CPT link
             */
            $title .= sprintf('<a href="%s" class="add-new-h2">%s</a>', esc_url(add_query_arg('page', 'wpcf-edit-type', admin_url('admin.php'))), __('Add New', 'wpcf'));
        } else {
            $title = __('Add New Custom Post Type', 'wpcf');
        }
    }
    wpcf_add_admin_header($title);
    wpcf_wpml_warning();
    $form = wpcf_form('wpcf_form_types');
    echo '<form method="post" action="" class="wpcf-types-form wpcf-form-validate js-types-do-not-show-modal">';
    echo $form->renderForm();
    echo '</form>';
    wpcf_add_admin_footer();
}