admin-install.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Custom CSS and JS
  4. *
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * CustomCSSandJS_Install
  11. */
  12. class CustomCSSandJS_Install {
  13. public static function install() {
  14. self::create_roles();
  15. self::register_post_type();
  16. flush_rewrite_rules();
  17. }
  18. /**
  19. * Create the custom-css-js post type
  20. */
  21. public static function register_post_type() {
  22. $labels = array(
  23. 'name' => _x( 'Custom Code', 'post type general name', 'custom-css-js'),
  24. 'singular_name' => _x( 'Custom Code', 'post type singular name', 'custom-css-js'),
  25. 'menu_name' => _x( 'Custom CSS & JS', 'admin menu', 'custom-css-js'),
  26. 'name_admin_bar' => _x( 'Custom Code', 'add new on admin bar', 'custom-css-js'),
  27. 'add_new' => _x( 'Add Custom Code', 'add new', 'custom-css-js'),
  28. 'add_new_item' => __( 'Add Custom Code', 'custom-css-js'),
  29. 'new_item' => __( 'New Custom Code', 'custom-css-js'),
  30. 'edit_item' => __( 'Edit Custom Code', 'custom-css-js'),
  31. 'view_item' => __( 'View Custom Code', 'custom-css-js'),
  32. 'all_items' => __( 'All Custom Code', 'custom-css-js'),
  33. 'search_items' => __( 'Search Custom Code', 'custom-css-js'),
  34. 'parent_item_colon' => __( 'Parent Custom Code:', 'custom-css-js'),
  35. 'not_found' => __( 'No Custom Code found.', 'custom-css-js'),
  36. 'not_found_in_trash' => __( 'No Custom Code found in Trash.', 'custom-css-js')
  37. );
  38. $capability_type = 'custom_css';
  39. $capabilities = array(
  40. 'edit_post' => "edit_{$capability_type}",
  41. 'read_post' => "read_{$capability_type}",
  42. 'delete_post' => "delete_{$capability_type}",
  43. 'edit_posts' => "edit_{$capability_type}s",
  44. 'edit_others_posts' => "edit_others_{$capability_type}s",
  45. 'publish_posts' => "publish_{$capability_type}s",
  46. 'read' => "read",
  47. 'delete_posts' => "delete_{$capability_type}s",
  48. 'delete_published_posts' => "delete_published_{$capability_type}s",
  49. 'delete_others_posts' => "delete_others_{$capability_type}s",
  50. 'edit_published_posts' => "edit_published_{$capability_type}s",
  51. 'create_posts' => "edit_{$capability_type}s",
  52. );
  53. $args = array(
  54. 'labels' => $labels,
  55. 'description' => __( 'Custom CSS and JS code', 'custom-css-js' ),
  56. 'public' => false,
  57. 'publicly_queryable' => false,
  58. 'show_ui' => true,
  59. 'show_in_menu' => true,
  60. 'menu_position' => 100,
  61. 'menu_icon' => 'dashicons-plus-alt',
  62. 'query_var' => false,
  63. 'rewrite' => array( 'slug' => 'custom-css-js' ),
  64. 'capability_type' => $capability_type,
  65. 'capabilities' => $capabilities,
  66. 'has_archive' => true,
  67. 'hierarchical' => false,
  68. 'exclude_from_search' => true,
  69. 'menu_position' => null,
  70. 'can_export' => false,
  71. 'supports' => array( 'title' )
  72. );
  73. register_post_type( 'custom-css-js', $args );
  74. }
  75. /**
  76. * Create roles and capabilities.
  77. */
  78. public static function create_roles() {
  79. global $wp_roles;
  80. if ( !current_user_can('update_plugins') )
  81. return;
  82. if ( ! class_exists( 'WP_Roles' ) ) {
  83. return;
  84. }
  85. if ( ! isset( $wp_roles ) ) {
  86. $wp_roles = new WP_Roles();
  87. }
  88. if ( isset($wp_roles->roles['css_js_designer']))
  89. return;
  90. // Add Web Designer role
  91. add_role( 'css_js_designer', __( 'Web Designer', 'custom-css-js'), array() );
  92. $capabilities = array();
  93. $capability_types = array( 'custom_css' );
  94. foreach ( $capability_types as $capability_type ) {
  95. $capabilities[ $capability_type ] = array(
  96. // Post type
  97. "edit_{$capability_type}",
  98. "read_{$capability_type}",
  99. "delete_{$capability_type}",
  100. "edit_{$capability_type}s",
  101. "edit_others_{$capability_type}s",
  102. "publish_{$capability_type}s",
  103. "delete_{$capability_type}s",
  104. "delete_published_{$capability_type}s",
  105. "delete_others_{$capability_type}s",
  106. "edit_published_{$capability_type}s",
  107. );
  108. }
  109. foreach ( $capabilities as $cap_group ) {
  110. foreach ( $cap_group as $cap ) {
  111. $wp_roles->add_cap( 'css_js_designer', $cap );
  112. $wp_roles->add_cap( 'administrator', $cap );
  113. }
  114. }
  115. }
  116. }
  117. ?>