Make WordPress Core

Ticket #14966: 14966.diff

File 14966.diff, 13.3 KB (added by nacin, 14 years ago)

Suggested second pass.

  • wp-includes/post.php

     
    47804780 * @since 3.1.0
    47814781 *
    47824782 * @param array $args Arguments.
    4783  * @param string $post_type Post type.
    47844783 */
    4785 function wp_quickpress_form( $args = array(), $post_type = 'post'){
    4786         global $post_ID;
     4784function wp_quickpress_form( $args = array() ) {
     4785        $defaults = array(
     4786                'form_id' => '',
     4787                'action' => admin_url( 'post.php' ),
     4788                'post' => null, // Post object, ID, or null.
     4789                'post_type' => null, // Post type. Optional. Overridden if 'post' parameter is specified.
     4790                'fields' => null, // Array of fields.
     4791                'submit_fields' => null, // Array of submit, hidden, and publishing inputs.
     4792                'tabindex' => false, // @todo Can we just kill tabindex?
     4793                'capability' => 'edit_posts',
     4794                'submit_class' => 'submit',
     4795                'publish_action_container' => 'span',
     4796                'publish_action_id' => 'publishing-action',
     4797                'submit_fields_container' => 'p',
     4798                'submit_fields_container_class' => 'submit',
     4799        );
    47874800
    4788         $fields = array(
     4801        $args = wp_parse_args( $args, $defaults );
     4802
     4803        // @todo Kill the cap check and arg. Stick to pure presentation only.
     4804        // We can still handle them on a field-by-field basis.
     4805        if ( ! current_user_can( $args['capability'] ) ) {
     4806                do_action( 'quickpress_form_no_form', $args );
     4807                return;
     4808        }
     4809
     4810        if ( empty( $args['post'] ) ) {
     4811                if ( null === $args['post_type'] )
     4812                        $args['post_type'] = 'post';
     4813                $args['post'] = 0;
     4814        } else {
     4815                $post = get_post( $args['post'] );
     4816                $args['post_type'] = $post->post_type;
     4817        }
     4818
     4819        $post_type_obj = get_post_type_object( $args['post_type'] );
     4820
     4821        // @todo Clean these up still. Arrays?
     4822        $default_fields = array(
    47894823                'title' => array(
    47904824                        'capability' => '', // Capability to check before outputing field
    4791                         'output' => '<h4 id="%s-title"><label for="title">'. __('Title') .'</label></h4>
     4825                        'output' => '<h4 id="%1$s-title"><label for="title">'. __('Title') .'</label></h4>
    47924826                <div class="input-text-wrap">
    4793                         <input type="text" name="post_title" id="%s-title" tabindex="%d" autocomplete="off" value="'. esc_attr( $post->post_title ).'" />
     4827                        <input type="text" name="post_title" id="%1$s-title" tabindex="%2$d" autocomplete="off" value="' . ( $post && esc_attr( $post->post_title ) ) . '" />
    47944828                </div>'
    47954829                ),
     4830                // @todo This needs JS enqueued to work.
    47964831                'media_buttons' => array(
    47974832                        'capability' => 'upload_files',
    4798                         'output' => '<div id="%s-media-buttons" class="hide-if-no-js">'. get_media_buttons() .'</div>',
     4833                        'output' => '<div id="%1$s-media-buttons" class="hide-if-no-js">'. get_media_buttons() .'</div>',
    47994834                ),
     4835                // @todo The TinyMCE stuff should disappear into a hook, like the ajax spinner.
    48004836                'content' => array(
    48014837                        'capability' => '',
    4802                         'output' => '<h4 id="%s-content-label"><label for="content">'. __('Content') .'</label></h4>
     4838                        'output' => '<h4 id="%1$s-content-label"><label for="content">'. __('Content') .'</label></h4>
    48034839                <div class="textarea-wrap">
    4804                         <textarea name="content" id="%s-content" class="mceEditor" rows="3" cols="15" tabindex="%d">'. $post->post_content.'</textarea>
     4840                        <textarea name="content" id="%1$s-content" class="mceEditor" rows="3" cols="15" tabindex="%2$d">' . ( $post && $post->post_content ) . '</textarea>
    48054841                </div>
    4806                         '."     <script type='text/javascript'>edCanvas = document.getElementById('content');edInsertContent = null;</script>
     4842                        '."     <script type='text/javascript'>edCanvas = document.getElementById('%1$s-content');edInsertContent = null;</script>
    48074843                "
    4808 
    48094844                ),
    48104845                'tags' => array(
    48114846                        'capability' =>'',
    48124847                        'output' => '
    4813                         <h4><label for="%s-tags-input">'. __('Tags') .'</label></h4>
     4848                        <h4><label for="%1$s-tags-input">'. __( 'Tags' ) .'</label></h4>
    48144849                        <div class="input-text-wrap">
    4815                                 <input type="text" name="%s-tags_input" id="tags-input" tabindex="%d" value="'. get_tags_to_edit( $post->ID ) .'" />
    4816                         </div>
    4817 '
     4850                                <input type="text" name="%1$s-tags_input" id="tags-input" tabindex="%2$d" value="' . ( $post && get_tags_to_edit( $post->ID ) ) . '" />
     4851                        </div>'
    48184852                ),
    4819 
    48204853        );
    48214854
    4822         $hidden_fields = array(
    4823                 'action' => '<input type="hidden" name="action" id="quickpost-action" value="'.$post_type.'-quickpress-save" />',
    4824                 'post_id' => '<input type="hidden" name="quickpress_post_ID" value="'. $post_ID .'" />',
    4825                 'post_type' => '<input type="hidden" name="post_type" value="'.$post_type.'" />',
    4826         );
     4855        if ( null === $args['fields'] ) {
     4856                $fields = $default_fields;
     4857        } else {
     4858                $fields = array();
     4859                foreach ( $args['fields'] as $key => $value ) {
     4860                        if ( true === $value ) // i.e. tags => true
     4861                                $fields[] = $default_fields[ $key ];
     4862                        elseif ( is_string( $value ) ) // i.e. array( 'tags' )
     4863                                $fields[] = $default_fields[ $value ];
     4864                        else
     4865                                $fields[] = $value;
     4866                }
     4867        }
     4868        // Sync this so filters are given correct information.
     4869        $args['fields'] = $fields;
    48274870
    4828         $submit_fields = array(
    4829                 'save' => '<input type="submit" name="save" id="save-post" class="button" tabindex="%s" value="'.  esc_attr('Save Draft') .'" />',
    4830                 'reset' => '<input type="reset" tabindex="%s" value="'. esc_attr( 'Reset' ).'" class="button" />',
     4871        $publishing_action = current_user_can( $post_type_obj->cap->publish_posts ) ? __( 'Publish' ) : __( 'Submit for Review' );
     4872
     4873        // These are keyed by input name.
     4874        // Accepted keys: type, value, id (defaults to none).
     4875        // These are also accepted for non-hidden inputs:
     4876        // primary (defaults to false), class (defaults to 'button', except for the primary button, when it is 'button-primary'),
     4877        // tabindex (defaults to true), accesskey (defaults to none).
     4878        $default_submit_fields = array(
     4879                'action'    => array( 'type' => 'hidden', 'id' => 'quickpost-action', 'value' => $post_type . '-quickpress-save' ),
     4880                'quickpress_post_ID' => array( 'type' => 'hidden', 'value' => $post_ID ),
     4881                'post_type' => array( 'type' => 'hidden', 'value' => $post_type ),
     4882                'save'      => array( 'type' => 'submit', 'value' => __( 'Save Draft' ) ),
     4883                'reset'     => array( 'type' => 'reset',  'value' => __( 'Reset' ) ),
     4884                'submit'    => array( 'type' => 'submit', 'primary' => true, 'accesskey' => 'p', 'value' => $publishing_action ),
    48314885        );
    48324886
    4833         $publishing_action = current_user_can('publish_posts') ? esc_attr('Publish') : esc_attr('Submit for Review');
     4887        if ( null === $args['submit_fields'] ) {
     4888                $submit_fields = $default_submit_fields;
     4889        } else {
     4890                $submit_fields = array();
     4891                foreach ( $args['submit_fields'] as $key => $value ) {
     4892                        // @todo If this is going to work like this, then the hidden/submit fields need to be split back up.
     4893                        if ( true === $value ) // i.e. tags => true
     4894                                $submit_fields[] = $default_submit_fields[ $key ];
     4895                        elseif ( is_string( $value ) ) // i.e. array( 'tags' )
     4896                                $submit_fields[] = $default_submit_fields[ $value ];
     4897                        else
     4898                                $submit_fields[] = $value;
     4899                }
     4900        }
    48344901
    4835         $publishing_fields = array(
    4836         'submit' => '<input type="submit" name="publish" id="publish" accesskey="p" tabindex="%s" class="button-primary" value="' . $publishing_action . '" />',
    4837         /*'test' => '<input type="submit" name="publish" id="publish" accesskey="p" tabindex="%n" class="button-primary" value="'. esc_attr('Publish') .'" />', */
     4902        $primary_fields = array();
     4903        foreach ( $submit_fields as $key => $f ) {
     4904                if ( ! isset( $f['type'] ) || ! isset( $f['value'] ) )
     4905                        continue;
     4906                $f['name'] = $key;
     4907                if ( ! isset( $f['id'] ) )
     4908                        $f['id'] = false;
    48384909
    4839         );
     4910                if ( 'hidden' == $f['type'] ) {
     4911                        $f['primary'] = $f['accesskey'] = $f['tabindex'] = $f['class'] = false;
     4912                } else {
     4913                        if ( ! isset( $f['class'] ) )
     4914                                $f['class'] = ! empty( $f['primary'] ) ? 'button-primary' : 'button';
     4915                        $f = wp_parse_args( $f, array( 'accesskey' => false, 'tabindex' => true, 'primary' => false, 'tabindex' => true ) );
     4916                }
     4917        }
    48404918
    4841         $defaults = array(
    4842                 'action' => admin_url( 'post.php' ),
    4843                 'fields' => $fields,
    4844                 'form_id' => '',
    4845                 'default_cap' => 'edit_posts',
    4846                 'tabindex_start' => '1',
    4847                 'ajax' => true,
    4848                 'hidden_fields' => $hidden_fields,
    4849                 'submit_fields' => $submit_fields,
    4850                 'publishing_fields' => $publishing_fields,
    4851                 'submit_class' => 'submit',
    4852                 'publish_action_container' => 'span',
    4853                 'publish_action_id' => 'publishing-action',
    4854                 'hidden_and_submit_fields_container' => 'p',
    4855                 'hidden_and_submit_fields_container_class' => 'submit',
    4856         );
     4919        // Sync this so filters are given correct information.
     4920        $args['submit_fields'] = $submit_fields;
     4921        // Then we can separate the primary fields.
     4922        foreach ( $submit_fields as $key => $f ) {
     4923                if ( $f['primary'] ) {
     4924                        $primary_fields[] = $f;
     4925                        unset( $submit_fields[ $key ] );
     4926                }
     4927        }
    48574928
    4858         $args = wp_parse_args($args, $defaults);
     4929        // @todo :(
     4930        $tabindex = apply_filters( 'quickpress_tabindex', $tabindex, $args );
    48594931
    4860         $tabindex =  apply_filters( 'quickpress_tabindex_start', $args['tabindex_start'], $args['form_id'] );
     4932        do_action('quickpress_before_form', $args );
    48614933
    4862         if ( current_user_can( $args['default_cap'] ) ): ?>
    4863                 <?php do_action('quickpress_form_before_form', $args['form_id'] ); ?>
    4864                 <form name="post" action="<?php echo $args['action'] ?>" method="post" id="<?php echo $args['form_id']; ?>">
    4865                         <?php do_action('quickpress_form_before_fields', $args['form_id']);
     4934        echo '<form name="post" action="' . $args['action'] . '" method="post" id="' . $args['form_id'] . '">';
    48664935
    4867                         $fields = apply_filters( 'quickpress_fields',  $args['fields'], $args['form_id'] );
    4868                         foreach ($fields as $title => $field){
    4869                                 if ( empty( $field['capability'] ) || current_user_can( $field['capability'] ) ){
    4870                                         printf( $field['output'], $args['form_id'], $args['form_id'], $tabindex );
    4871                                         $tabindex++;
    4872                                 }
    4873                         }
    4874                         //Hidden Fields
    4875                         do_action('quickpress_form_after_fields', $args['form_id'] );
     4936        do_action( 'quickpress_before_fields', $args );
    48764937
    4877                         echo "<{$args['hidden_and_submit_fields_container']} class='{$args['hidden_and_submit_fields_container_class']}'>";
     4938        $fields = apply_filters( 'quickpress_fields', $form_fields, $args );
     4939        foreach ( $fields as $title => $field ) {
     4940                if ( empty( $field['capability'] ) || current_user_can( $field['capability'] ) )
     4941                        printf( $field['output'], $args['form_id'], ( $tabindex ? $tabindex++ : '' ) );
     4942        }
    48784943
    4879                         $hidden_fields = apply_filters( 'quickpress_hidden_fields', $args['hidden_fields'] , $args['form_id'] );
     4944        do_action( 'quickpress_after_fields', $form_id );
    48804945
    4881                         foreach( $hidden_fields as $hidden_field )
    4882                                 echo $hidden_field;
     4946        echo "<{$args['submit_fields_container']} class='{$args['submit_fields_container_class']}'>";
    48834947
    4884                         // nonce
    4885                         wp_nonce_field('add-post');
     4948        // nonce
     4949        wp_nonce_field( 'add-post' );
    48864950
    4887                         // submit
    4888                         foreach( $args['submit_fields'] as $submit_field )
    4889                                 printf( $submit_field, $tabindex++ );
     4951        foreach ( array( $args['submit_fields'], $args['primary_fields'] ) as $fields ) {
     4952                foreach( $fields as $f ) {
     4953                        $f['tabindex']  = $tabindex && ! empty( $f['tabindex'] ) ? ' tabindex="' . $tabindex++ . '"'  : '';
     4954                        $f['class']     = ! empty( $f['class'] )     ? ' class="' . $f['class'] . '"' : '';
     4955                        $f['id']        = ! empty( $f['id'] )        ? ' id="' . $f['id'] . '"'       : '';
     4956                        $f['accesskey'] = ! empty( $f['accesskey'] ) ? ' accesskey="' . $f['accesskey'] . '"' : '';
    48904957
    4891                         // publish
    4892                         echo "<{$args['publish_action_container']} id='{$args['publish_action_id']}'>";
     4958                        $f['name']  = ' name="' . $f['name'] . '"';
     4959                        $f['type']  = ' type="' . $f['type'] . '"';
     4960                        $f['value'] = ' value="' . esc_attr( $f['value'] ) . '"';
     4961                }
     4962        }
    48934963
    4894                         $publishing_fields = apply_filters( 'quickpress_publishing_fields', $args['publishing_fields'] , $args['form_id'] );
     4964        // hidden fields and secondary submit buttons
     4965        foreach ( $args['submit_fields'] as $f )
     4966                echo '<input ' . $f['type'] . $f['name'] . $f['id'] . $f['class'] . $f['tabindex'] . $f['value'] . '" />';
    48954967
    4896                         foreach( $publishing_fields as $publishing_field) {
    4897                                 printf( $publishing_field, $tabindex );
    4898                                         $tabindex++;
    4899                         }
     4968        // primary submit buttons
     4969        echo "<{$args['publish_action_container']} id='{$args['publish_action_id']}'>";
    49004970
    4901                         if ($args['ajax'] == true)
    4902                                 echo '<img class="waiting" src="'. esc_url( admin_url( 'images/wpspin_light.gif' ) ) .'" />';
     4971        foreach ( $args['primary_fields'] as $f )
     4972                echo '<input ' . $f['type'] . $f['name'] . $f['id'] . $f['class'] . $f['tabindex'] . $f['value'] . '" />';
    49034973
    4904                         echo "</{$args['publish_action_container']}>";
    4905                         echo "<br class='clear' />";
    4906                         do_action( 'quickpress_form_after_submit_fields', $args['form_id']);
     4974        do_action( 'quickpress_publish_actions', $args );
    49074975
    4908                         echo "</{$args['hidden_and_submit_fields_container']}";
    4909                 do_action( 'quickpress_form_after_form_content', $args['form_id']);
    4910                 echo '</form>';
    4911                 do_action('quickpress_form_after_form', $args['form_id'] );
    4912         else:
    4913                 do_action( 'quickpress_form_no_form', $args['form_id'] );
    4914         endif;
     4976        echo "</{$args['publish_action_container']}>";
     4977
     4978        echo "<br class='clear' />";
     4979
     4980        do_action( 'quickpress_after_submit_fields', $args );
     4981
     4982        echo "</{$args['submit_fields_container']}";
     4983
     4984        do_action( 'quickpress_after_form_content', $args );
     4985
     4986        echo '</form>';
     4987
     4988        do_action( 'quickpress_after_form', $args );
    49154989}
    49164990
     4991// @todo Move this to admin/includes/dashboard.
     4992function _dashboard_quickpress_publish_actions( $args ) {
     4993        if ( ! isset( $args['form_id'] ) || $args['form_id'] != 'dashboard_quick-press' )
     4994                return;
     4995        echo '<img class="waiting" src="'. esc_url( admin_url( 'images/wpspin_light.gif' ) ) .'" />';
     4996}
     4997add_action( 'quickpress_publish_actions', '_dashboard_quickpress_publish_actions' );
     4998
    49174999?>
     5000 No newline at end of file