slug = $postType; $this->single = strtolower($single); $this->plural = strtolower($plural); $this->labels = $labels; $this->args = $args; add_action('init', array($this, 'register')); } public static function addMetaBox($metaBox) { if (!class_exists($metaBox)) { $metaBox = '\\Module\\' . $metaBox; } static::$instance->metaBox = new $metaBox(static::$instance); } public function register() { $plural = $this->plural; $single = $this->single; $postType = $this->slug; Logger::write($postType); $textDomain = Config::get('text-domain'); $dlabels = array( 'name' => _x(ucfirst($plural), 'post type general name', $textDomain), 'singular_name' => _x(ucfirst($single), 'post type singular name', $textDomain), 'menu_name' => _x(ucfirst($plural), 'admin menu', $textDomain), 'name_admin_bar' => _x(ucfirst($single), 'add new on admin bar', $textDomain), 'add_new' => _x('Add new', '', $textDomain), 'add_new_item' => __('Add new ' . $single, $textDomain), 'new_item' => __('New ' . $single, $textDomain), 'edit_item' => __('Edit ' . $single, $textDomain), 'view_item' => __('View ' . $single, $textDomain), 'all_items' => __('All ' . $plural, $textDomain), 'search_items' => __('Search ' . $plural, $textDomain), 'parent_item_colon' => __('Parent ' . $plural . ':', $textDomain), 'not_found' => __('No ' . $plural . ' found.', $textDomain), 'not_found_in_trash' => __('No ' . $plural . ' found in Trash.', $textDomain) ); foreach ($this->labels as $key => $value) { $dlabels[$key] = $value; } $dargs = array( 'labels' => $dlabels, 'description' => __('Description.', $textDomain), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => false, 'query_var' => true, 'rewrite' => array('slug' => $postType), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 105, 'supports' => array('title', 'editor') ); foreach ($this->args as $key => $value) { $dargs[$key] = $value; } register_post_type($postType, $dargs); } public function getSlug() { return $this->slug; } public function getName() { return array( 'singular' => $this->single, 'plural' => $this->plural ); } public static function insert($title, $content = '') { if (post_exists($title)) { $post = get_page_by_title($title, OBJECT, static::$instance->slug); return $post->ID; } $post = array( 'post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => static::$instance->slug, ); $id = wp_insert_post($post); return $id; } /** * WP_Query based method. See codex for field names. * * @param string $field * @param type $value * @param string $orderby * @return array of WP_Posts */ public static function getBy($field, $value, $orderby = 'date') { $args = array( 'posts_per_page' => -1, 'offset' => 0, 'orderby' => $orderby, 'order' => 'DESC', 'post_type' => static::$instance->slug, $field => $value ); $response = new \WP_Query($args); return $response->posts; } }