Making WordPress.org

Changeset 323


Ignore:
Timestamp:
01/29/2014 05:37:09 AM (12 years ago)
Author:
nacin
Message:

Trac component pages: Support subcomponents.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php

    r322 r323  
    66        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
    77        add_action( 'the_content', array( $this, 'the_content' ), 5 );
    8         add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
     8        add_action( 'save_post_component', array( $this, 'save_post' ), 10, 2 );
    99        $this->trac = $GLOBALS['wpdb'];
    1010    }
     
    5252    }
    5353
     54    function page_is_component( $post ) {
     55        $post = get_post( $post );
     56        if ( $post->post_type != 'component' ) {
     57            return false;
     58        }
     59        if ( $post->post_parent == 0 ) {
     60            return true;
     61        }
     62        if ( get_post_meta( $post->ID, '_page_is_subcomponent', true ) ) {
     63            return true;
     64        }
     65        return false;
     66    }
     67
    5468    function register_meta_box_cb( $post ) {
    55         if ( $post->post_parent == 0 && $post->post_status !== 'auto-draft' ) {
     69        if ( $post->post_status !== 'auto-draft' ) {
    5670            add_meta_box( 'component-settings', 'Settings', array( $this, 'meta_box_cb' ) );
    5771        }
     
    5973
    6074    function meta_box_cb( $post ) {
     75        wp_nonce_field( 'component-settings_' . $post->ID, 'component-settings-nonce', false );
     76        if ( $post->post_parent != 0 ) {
     77            $checked = checked( (bool) get_post_meta( $post->ID, '_page_is_subcomponent', true ), true, false );
     78            echo '<p><label for="page-is-subcomponent"><input type="checkbox"' . $checked . ' name="page-is-subcomponent" id="page-is-subcomponent" /> This page is a subcomponent</label></p>';
     79        }
     80        if ( ! $this->page_is_component( $post ) ) {
     81            return;
     82        }
    6183        $value = get_post_meta( $post->ID, '_active_maintainers', true );
    6284        echo '<p><label for="active-maintainers">Active maintainers (WP.org usernames, comma-separated)</label> <input type="text" class="large-text" id="active-maintainers" name="active-maintainers" value="' . esc_attr( $value ) . '" />';
     
    6486
    6587    function save_post( $post_id, $post ) {
    66         if ( $post->post_type !== 'component' || $post->post_parent != 0 ) {
     88        if ( ! isset( $_POST['component-settings-nonce'] ) ) {
    6789            return;
    6890        }
    69         if ( ! isset( $_POST['active-maintainers'] ) ) {
     91
     92        if ( ! wp_verify_nonce( $_POST['component-settings-nonce'], 'component-settings_' . $post->ID ) ) {
    7093            return;
    7194        }
    72         update_post_meta( $post->ID, '_active_maintainers', sanitize_text_field( wp_unslash( $_POST['active-maintainers'] ) ) );
     95
     96        if ( $post->post_parent != 0 ) {
     97            update_post_meta( $post->ID, '_page_is_subcomponent', isset( $_POST['page-is-subcomponent'] ) );
     98        }
     99
     100        if ( isset( $_POST['active-maintainers'] ) ) {
     101            update_post_meta( $post->ID, '_active_maintainers', sanitize_text_field( wp_unslash( $_POST['active-maintainers'] ) ) );
     102        }
    73103    }
    74104
     
    79109    function the_content( $content ) {
    80110        $post = get_post();
    81         if ( $post->post_type !== 'component' || $post->post_parent != 0 ) {
     111        if ( ! $this->page_is_component( $post ) ) {
    82112            return $content;
    83         }
    84 
     113        }   
     114
     115        if ( $post->post_parent ) {
     116            $top_level = '<h4>This is a subcomponent of the <a href="' . get_permalink( $post->post_parent ) . '">' . get_post( $post->post_parent )->post_title . '</a> component.</h4>';
     117            $content = $top_level . "\n\n" . $content;
     118        }
    85119        ob_start();
    86120?>
     
    123157        }
    124158
    125         $sub_pages = wp_list_pages( array( 'child_of' => $post->ID, 'post_type' => 'component', 'echo' => false, 'title_li' => false ) );
     159        $subcomponents_query = new WP_Query( array(
     160            'post_type' => 'component',
     161            'post_status' => 'publish',
     162            'post_parent' => $post->ID,
     163            'update_post_term_cache' => false,
     164            'update_post_meta_cache' => false,
     165            'meta_key' => '_page_is_subcomponent',
     166            'meta_value' => '1',
     167        ) );
     168
     169        if ( $subcomponents_query->have_posts() ) {
     170            echo '<h4>Subcomponents: ';
     171            $subcomponents = array();
     172            foreach ( $subcomponents_query->posts as $subcomponent ) {
     173                $subcomponents[ $subcomponent->ID ] = '<a href="' . get_permalink( $subcomponent ) . '">' . $subcomponent->post_title . '</a>';
     174            }
     175            echo implode( ', ', $subcomponents ) . '</h4>';
     176        }
     177
     178
     179        $sub_pages = wp_list_pages( array( 'child_of' => $post->ID, 'post_type' => 'component', 'echo' => false, 'title_li' => false, 'exclude' => implode( ',', array_keys( $subcomponents ) ) ) );
    126180        if ( $sub_pages ) {
    127181            echo "<h3>Pages under " . get_the_title() . "</h3>\n";
Note: See TracChangeset for help on using the changeset viewer.