示例#1
0
function wpshop_tool_default_datas_check()
{
    $output_ok = $output_error = '';
    /**	Get defined default datas type	*/
    $default_custom_post_type = unserialize(WPSHOP_DEFAULT_CUSTOM_TYPES);
    /**	Read the default data saved to check	*/
    if (!empty($default_custom_post_type)) {
        foreach ($default_custom_post_type as $type) {
            $has_error = false;
            $file_uri = WPSHOP_TEMPLATES_DIR . 'default_datas/' . $type . '.csv';
            if (is_file($file_uri)) {
                unset($tpl_component);
                $tpl_component = array();
                $tpl_component['CUSTOM_POST_TYPE_NAME'] = 'wpshop_cpt_' . $type;
                /**	Launch check on custom post type	*/
                $check_cpt = wpshop_entities::check_default_custom_post_type($type, $tpl_component);
                $has_error = $check_cpt[0];
                $tpl_component['TOOLS_CUSTOM_POST_TYPE_CONTAINER'] = $check_cpt[1];
                $tpl_component = array_merge($tpl_component, $check_cpt[2]);
                if ($has_error) {
                    $output_error .= wpshop_display::display_template_element('wpshop_admin_tools_default_datas_check_main_element', $tpl_component, array(), 'admin');
                } else {
                    $output_ok .= wpshop_display::display_template_element('wpshop_admin_tools_default_datas_check_main_element', $tpl_component, array(), 'admin');
                }
            }
        }
    }
    echo wpshop_display::display_template_element('wpshop_admin_tools_default_datas_check_main', array('TOOLS_CUSTOM_POST_TYPE_LIST' => $output_error . $output_ok), array(), 'admin');
    die;
}
 /**
  * Allows to create a new custom post type from a csv file, allowing to create default entities or import new entities
  *
  * @param string $identifier The custom post type identifier. This identifier is unique into database
  *
  * @return array The different response element for the request. $result: Boolean representing if creation is OK / $container: Where the result must be placed into output code / $output: The html content to output
  */
 public static function create_cpt_from_csv_file($identifier, $custom_file = '')
 {
     global $wpdb;
     $output = '';
     $container = '';
     $result = true;
     $custom_post_type_default_structure = array('post_title' => 'mandatory', 'post_name' => 'mandatory', 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ENTITIES);
     /**	Check custom post type exsitance	*/
     $query = $wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = %s", $identifier);
     $custom_post_type_identifier = $wpdb->get_var($query);
     $container = 'wpshop_cpt_' . $identifier;
     $file_uri = !empty($custom_file) ? $custom_file : WPSHOP_TEMPLATES_DIR . 'default_datas/' . $identifier . '.csv';
     if (is_file($file_uri) && empty($custom_post_type_identifier)) {
         $csv_file_default_data = file($file_uri, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
         $db_field_definition = explode(";", $csv_file_default_data[0]);
         $db_datas_definition = explode(";", $csv_file_default_data[1]);
         $has_error = false;
         $errors = array();
         foreach ($custom_post_type_default_structure as $field_name => $field_default_value) {
             if (!in_array(str_replace('post_', '', $field_name), $db_field_definition)) {
                 if ($field_name == 'post_name') {
                     $db_datas_definition[] = $identifier;
                     $db_field_definition[] = str_replace('post_', '', $field_name);
                 } else {
                     if ($field_default_value == 'mandatory') {
                         $has_error = true;
                         $errors[] = $field_name;
                     } else {
                         $db_datas_definition[] = $field_default_value;
                         $db_field_definition[] = str_replace('post_', '', $field_name);
                     }
                 }
             }
         }
         if ($has_error) {
             $result = false;
             $output = sprintf(__('You have to fill %s, they are mandatory for custom type creation', 'wpshop'), implode(',', $errors));
         } else {
             $custom_post_type_def = array();
             foreach ($db_field_definition as $field_position => $field_name) {
                 $custom_post_type_def['post_' . $field_name] = $db_datas_definition[$field_position];
             }
             $new_custom_post_type = wp_insert_post($custom_post_type_def);
             if (is_int($new_custom_post_type) && !empty($new_custom_post_type)) {
                 $result = true;
             }
             $check_cpt = wpshop_entities::check_default_custom_post_type($identifier, array(), $result, $custom_file);
             $output = $check_cpt[1];
         }
     }
     return array($result, $container, $output);
 }