Advertisement
prefixaut

Untitled

Jun 20th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.08 KB | None | 0 0
  1. <?php
  2. if (!function_exists("pre_posttype")):
  3. function pre_posttype() {
  4.     $types = array(
  5.         "pre_selection" => array(
  6.             "single" => "Selection",
  7.             "description" => "A Selection of items which will appear on the front-page",
  8.             "public" => false,
  9.             "publicly_queryable" => true,
  10.             "show_ui" => true,
  11.             "show_in_nav_menus" => false,
  12.             "show_in_menu" => true,
  13.             "show_in_admin_bar" => false,
  14.             "menu_position" => 30,
  15.             "menu_icon" => "dashicons-visibility",
  16.             "hierachical" => false,
  17.             "supports" => array("title", "editor")
  18.         ),
  19.         "pre_project" => array(
  20.             "single" => "Project",
  21.             "description" => "(Programming-) Projects which this Website shows",
  22.             "public" => true,
  23.             "show_ui" => true,
  24.             "show_in_nav_menus" => true,
  25.             "show_in_menu" => true,
  26.             "show_in_admin_bar" => true,
  27.             "menu_position" => 31,
  28.             "menu_icon" => "dashicons-category",
  29.             "hierachical" => false,
  30.             "supports" => array("title", "editor", "thumbnail")
  31.         ),
  32.         "pre_template" => array(
  33.             "single" => "Template",
  34.             "description" => "Template-Project for streaming",
  35.             "public" => true,
  36.             "show_ui" => true,
  37.             "show_in_nav_menus" => true,
  38.             "show_in_menu" => true,
  39.             "show_in_admin_bar" => true,
  40.             "menu_position" => 32,
  41.             "hierachical" => false,
  42.             "supports" => array("title", "editor")
  43.         )
  44.     );
  45.    
  46.     foreach ($types as $name => $t) {
  47.         # Saving SINGLE and MULTI in variables and unsetting them, to avoid problems since we merge and post it
  48.        # When neither is set, we'll create them from the name
  49.        $s = isset($t["single"]) ? $t["single"] : (ucfirst(strtolower(strpos($name, "_") ? substr(strpos($name, "_")+1) : $name)));
  50.         $m = isset($t["multi"]) ? $t["multi"] : (strcasecmp($s{strlen($s)-1},"s") ? $s : "{$s}s");
  51.         # Un-setting extra values so they won't show up on the wp-registering and may screw shit up.
  52.        unset($t["multi"], $t["single"]);
  53.        
  54.         register_post_type($name, PreLib::merge_all($t, array(
  55.             # Defaults which will be applied when nothing is specified
  56.            "label" => $m,
  57.             "labels" => array(
  58.                 "name"                  => $m,
  59.                 "singular_name"         => $s,
  60.                 "add_new"               => "Add New",
  61.                 "add_new_item"          => "Add new {$s}",
  62.                 "edit_item"             => "Edit {$s}",
  63.                 "new_item"              => "New {$s}",
  64.                 "view_item"             => "View {$s}",
  65.                 "search_items"          => "Search {$s}",
  66.                 "not_found"             => "Nothing Found",
  67.                 "not_found_in_trash"    => "Nothing found in Trash",
  68.                 "parent_item_colon"     => "Parent {$s}",
  69.                 "all_items"             => "All {$m}",
  70.                 "archives"              => "{$s} Archives",
  71.                 "insert_into_item"      => "Insert into {$s}",
  72.                 "uploaded_to_this_item" => "Uploaded to this {$s}",
  73.                 "menu_name"             => $m,
  74.                 "update_item"           => "Update {$s}",
  75.             ),
  76.             "capability_type" => array(
  77.                 strtolower($s),
  78.                 strtolower($m)
  79.             ),
  80.             "rewrite" => array(
  81.                 "slug" => strtolower($s)
  82.             ),
  83.             "query_var" => $s,
  84.             "rest_base" => strtolower($s)
  85.         )));
  86.     }
  87. }
  88. endif;
  89. add_action("init", "pre_posttype");
  90.  
  91. /* Merge all Function from the PreLib class */
  92. public static function merge_all() {
  93.        # If there are less than 2 arrays we can't merge
  94.       if (func_num_args() < 2) throw new invalidargumentexception("This method requires at least two or more arrays as arguments!");
  95.        # Always the first
  96.       $main = func_get_arg(0);
  97.        # 2nd or all others
  98.       $sub = func_num_args() == 2 ? func_get_arg(1) : array_slice(func_get_args(), 1, func_num_args());
  99.        # Array check
  100.       if (!is_array($main) || !is_array($sub)) throw new invalidargumentexception("This method requires all arguments to be arrays!");
  101.        
  102.        if (func_num_args() > 2) {
  103.            # Iterate through all subs and call this method one after another
  104.           foreach ($sub as $s) $main = merge_all($main, $s);
  105.            return $main;
  106.        }
  107.        
  108.        foreach ($sub as $key => $val) {
  109.            # Main has same val as sub, next
  110.           if ($main[$key] == $val) continue;
  111.            # Main doesn't have the key, so set it
  112.           if (!isset($main[$key])) {
  113.                $main[$key] = $sub[$key];
  114.                continue;
  115.            }
  116.            # Merge all sub-arrays as well
  117.           if (is_array($val) && is_array($main[$key])) $main[$key] = merge_all($main[$key], $val);
  118.        }
  119.        
  120.        return $main;
  121.    }
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement