Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace City;
- function setup() {
- add_action( 'init', __NAMESPACE__ . '\store_city' );
- add_action( 'add_meta_boxes', __NAMESPACE__ . '\store_meta_city' );
- add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\admin_scripts', 11 );
- add_action( 'admin_print_scripts', __NAMESPACE__ . '\admin_templates' );
- add_action( 'wp_ajax_index_postmeta', __NAMESPACE__ . '\index_postmeta' );
- add_action( 'wp_ajax_update_postmeta', __NAMESPACE__ . '\update_postmeta' );
- }
- function store_city() {
- $labels = array(
- 'name' => __( 'Cities', 'WP_LANCASTER' ),
- 'singular_name' => __( 'City', 'WP_LANCASTER' ),
- 'add_new' => __( 'Add New City', 'WP_LANCASTER' ),
- 'add_new_item' => __( 'Add New City', 'WP_LANCASTER' ),
- 'new_item' => __( 'New City', 'WP_LANCASTER' ),
- 'edit_item' => __( 'Edit City', 'WP_LANCASTER' ),
- 'view_item' => __( 'View City', 'WP_LANCASTER' ),
- 'all_items' => __( 'All Cities', 'WP_LANCASTER' ),
- 'search_items' => __( 'Search Cities', 'WP_LANCASTER' ),
- 'not_found' => __( 'No Cities found', 'WP_LANCASTER' ),
- 'not_found_in_trash' => __( 'No Cities found in Trash', 'WP_LANCASTER' ),
- );
- $args = array(
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'query_var' => true,
- 'label' => __( 'city', 'WP_LANCASTER' ),
- 'labels' => $labels,
- 'capability_type' => 'post',
- 'has_archive' => false
- );
- register_post_type( 'city', $args );
- }
- /**
- * Create Meta boxes for the City cpt
- */
- function store_meta_city() {
- add_meta_box(
- 'city_streets',
- 'City Streets',
- __NAMESPACE__ . '\city_streets_callback',
- 'city',
- 'normal',
- 'default'
- );
- }
- /**
- * Callback func for Meta box
- */
- function city_streets_callback( $post, $metabox ) {
- wp_nonce_field( 'index_get', 'city_streets_get_nonce' );
- ?>
- <div id="city_streets_container">
- <div id="create_streets">
- <p></p>
- <input class="widefat" name="create_streets_field" placeholder="Enter New Street" id="create_streets_field">
- <p></p>
- <button class="button button-primary button-large" id="create_streets_submit">Add New Street</button>
- </div>
- <ul id="list_streets">
- </ul>
- </div>
- <?php
- }
- /**
- * Enqueue scripts for back-end.
- *
- */
- function admin_scripts() {
- $post = get_post();
- if ( $post && "city" === $post->post_type ) {
- wp_enqueue_style( 'city-admin', get_template_directory_uri() . "/css/city.css" );
- wp_enqueue_script(
- 'city-admin',
- get_template_directory_uri() . "/js/city.js",
- array( 'jquery', 'backbone', 'underscore' ),
- '0.0.1',
- true);
- wp_localize_script( 'city-admin', 'sbPostObject', get_object_vars( $post ) );
- }
- }
- /**
- * Index of new CPT. This will show posts in a post type
- */
- function index_postmeta() {
- $nonce = isset( $_REQUEST['nonce'] ) ? $_REQUEST['nonce'] : '';
- $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : false;
- $key = isset( $_REQUEST['key'] ) ? esc_html( $_REQUEST['key'] ) : false;
- if ( ! wp_verify_nonce( $nonce, 'index_get' ) ) {
- wp_send_json_error( 'Insecure Request' );
- }
- if ( isset( $id, $key ) && 'city_streets' === $key ) {
- $meta = get_post_meta( $id, $key, true );
- wp_send_json_success( $meta );
- } else {
- // If the nonce was invalid or data was not complete
- wp_send_json_error( 'Not a complete request' );
- }
- }
- /**
- * Update existing Post meta.
- */
- function update_postmeta() {
- $nonce = isset( $_REQUEST['nonce'] ) ? $_REQUEST['nonce'] : '';
- $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : false;
- $key = isset( $_REQUEST['key'] ) ? esc_html( $_REQUEST['key'] ) : false;
- if ( ! wp_verify_nonce( $nonce, 'index_get' ) ) {
- wp_send_json_error( 'Insecure Request' );
- }
- if ( isset( $id, $key ) && 'city_streets' === $key ) {
- if ( ! isset( $_REQUEST['value'] ) ) {
- $update = delete_post_meta( $id, $key );
- } else {
- $update = update_post_meta( $id, $key, $_REQUEST['value'] );
- }
- wp_send_json_success( $update );
- } else {
- // If the nonce was invalid or data was not complete
- wp_send_json_error( 'Not a complete request' );
- }
- }
- /**
- * Display JS templates in admin footer
- *
- * @since 0.3.0
- *
- * @return void
- *
- */
- function admin_templates() {
- ?>
- <script type="text/html" id="tmpl-city-streets">
- <p class='post'><%= title %></p>
- <div class='actions'>
- <a href="#" class='edit'>Edit</a><span>|</span>
- <a href="#" class='delete'>delete</a>
- </div>
- </script>
- <script type="text/html" id="tmpl-city-streets-edit">
- <div class="clearfix">
- <p class='post'><%= title %></p>
- <div class='actions'>
- <a href="#" class='cancel'>Cancel</a><span>|</span>
- <a href="#" class='delete'>delete</a>
- </div>
- </div>
- <div class="edit-container clearfix">
- <input type="text" placeholder="street-name" class="widefat" value="<%= title %>"/>
- <div class='actions'>
- <a href="#" class="cancel">Cancel</a>
- <button class='button update'>Update</button>
- </div>
- </div>
- </script>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement