~canonical-sysadmins/wordpress/4.5.2

« back to all changes in this revision

Viewing changes to wp-admin/includes/post.php

  • Committer: Manuel Seelaus
  • Date: 2015-12-09 17:47:18 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: manuel.seelaus@canonical.com-20151209174718-coxethm2swbeqksy
Merge WP4.4 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
173
173
 *
174
174
 * @since 1.5.0
175
175
 *
176
 
 * @global wpdb $wpdb
 
176
 * @global wpdb $wpdb WordPress database abstraction object.
177
177
 *
178
178
 * @param array $post_data Optional.
179
179
 * @return int Post ID.
371
371
        if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
372
372
                $fields = array( 'post_title', 'post_content', 'post_excerpt' );
373
373
 
374
 
                foreach( $fields as $field ) {
 
374
                foreach ( $fields as $field ) {
375
375
                        if ( isset( $post_data[ $field ] ) ) {
376
376
                                $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
377
377
                        }
403
403
 *
404
404
 * @since 2.7.0
405
405
 *
406
 
 * @global wpdb $wpdb
 
406
 * @global wpdb $wpdb WordPress database abstraction object.
407
407
 *
408
408
 * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
409
409
 * @return array
577
577
 *
578
578
 * @since 2.0.0
579
579
 *
580
 
 * @param string $post_type A post type string, defaults to 'post'.
 
580
 * @param string $post_type    Optional. A post type string. Default 'post'.
 
581
 * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
581
582
 * @return WP_Post Post object containing all the default post data as attributes
582
583
 */
583
584
function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
658
659
 *
659
660
 * @since 2.0.0
660
661
 *
661
 
 * @global wpdb $wpdb
 
662
 * @global wpdb $wpdb WordPress database abstraction object.
662
663
 *
663
664
 * @param string $title Post title
664
665
 * @param string $content Optional post content
842
843
 *
843
844
 * @since 1.2.0
844
845
 *
845
 
 * @global wpdb $wpdb
 
846
 * @global wpdb $wpdb WordPress database abstraction object.
846
847
 *
847
848
 * @return mixed
848
849
 */
875
876
 *
876
877
 * @since 1.2.0
877
878
 *
878
 
 * @global wpdb $wpdb
 
879
 * @global wpdb $wpdb WordPress database abstraction object.
879
880
 *
880
881
 * @param int $postid
881
882
 * @return mixed
1003
1004
                $perm = 'readable';
1004
1005
        }
1005
1006
 
1006
 
        if ( isset($q['orderby']) )
 
1007
        if ( isset( $q['orderby'] ) ) {
1007
1008
                $orderby = $q['orderby'];
1008
 
        elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
 
1009
        } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) {
1009
1010
                $orderby = 'modified';
 
1011
        }
1010
1012
 
1011
 
        if ( isset($q['order']) )
 
1013
        if ( isset( $q['order'] ) ) {
1012
1014
                $order = $q['order'];
1013
 
        elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
 
1015
        } elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) {
1014
1016
                $order = 'ASC';
 
1017
        }
1015
1018
 
1016
1019
        $per_page = "edit_{$post_type}_per_page";
1017
1020
        $posts_per_page = (int) get_user_option( $per_page );
1067
1070
 *
1068
1071
 * @since 2.5.0
1069
1072
 *
1070
 
 * @global wpdb $wpdb
 
1073
 * @global wpdb $wpdb WordPress database abstraction object.
1071
1074
 *
1072
1075
 * @param string $type
1073
1076
 * @return mixed
1123
1126
                unset($q['post_mime_type']);
1124
1127
        }
1125
1128
 
1126
 
        foreach( array_keys( $post_mime_types ) as $type ) {
 
1129
        foreach ( array_keys( $post_mime_types ) as $type ) {
1127
1130
                if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) {
1128
1131
                        $q['post_mime_type'] = $type;
1129
1132
                        break;
1242
1245
                }
1243
1246
 
1244
1247
                /** This filter is documented in wp-admin/edit-tag-form.php */
1245
 
                $uri = apply_filters( 'editable_slug', $uri );
 
1248
                $uri = apply_filters( 'editable_slug', $uri, $post );
1246
1249
                if ( !empty($uri) )
1247
1250
                        $uri .= '/';
1248
1251
                $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
1249
1252
        }
1250
1253
 
1251
1254
        /** This filter is documented in wp-admin/edit-tag-form.php */
1252
 
        $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) );
 
1255
        $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
1253
1256
        $post->post_status = $original_status;
1254
1257
        $post->post_date = $original_date;
1255
1258
        $post->post_name = $original_name;
1256
1259
        unset($post->filter);
1257
1260
 
1258
 
        return $permalink;
 
1261
        /**
 
1262
         * Filter the sample permalink.
 
1263
         *
 
1264
         * @since 4.4.0
 
1265
         *
 
1266
         * @param string  $permalink Sample permalink.
 
1267
         * @param int     $post_id   Post ID.
 
1268
         * @param string  $title     Post title.
 
1269
         * @param string  $name      Post name (slug).
 
1270
         * @param WP_Post $post      Post object.
 
1271
         */
 
1272
        return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
1259
1273
}
1260
1274
 
1261
1275
/**
1275
1289
 
1276
1290
        list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
1277
1291
 
 
1292
        $view_link = false;
 
1293
        $preview_target = '';
 
1294
 
1278
1295
        if ( current_user_can( 'read_post', $post->ID ) ) {
1279
 
                $ptype = get_post_type_object( $post->post_type );
1280
 
                $view_post = $ptype->labels->view_item;
1281
 
        }
1282
 
 
1283
 
        if ( 'publish' == get_post_status( $post ) ) {
1284
 
                $title = __('Click to edit this part of the permalink');
1285
 
        } else {
1286
 
                $title = __('Temporary permalink. Click to edit this part.');
1287
 
        }
1288
 
 
 
1296
                if ( 'draft' === $post->post_status ) {
 
1297
                        $draft_link = set_url_scheme( get_permalink( $post->ID ) );
 
1298
                        $view_link = get_preview_post_link( $post, array(), $draft_link );
 
1299
                        $preview_target = " target='wp-preview-{$post->ID}'";
 
1300
                } else {
 
1301
                        if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
 
1302
                                $view_link = get_permalink( $post );
 
1303
                        } else {
 
1304
                                // Allow non-published (private, future) to be viewed at a pretty permalink.
 
1305
                                $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, urldecode( $permalink ) );
 
1306
                        }
 
1307
                }
 
1308
        }
 
1309
 
 
1310
        // Permalinks without a post/page name placeholder don't have anything to edit
1289
1311
        if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
1290
 
                $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
 
1312
                $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
 
1313
 
 
1314
                if ( false !== $view_link ) {
 
1315
                        $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $view_link . "</a>\n";
 
1316
                } else {
 
1317
                        $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
 
1318
                }
 
1319
 
 
1320
                // Encourage a pretty permalink setting
1291
1321
                if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
1292
1322
                        $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
1293
1323
                }
1294
1324
        } else {
1295
1325
                if ( function_exists( 'mb_strlen' ) ) {
1296
 
                        if ( mb_strlen( $post_name ) > 30 ) {
1297
 
                                $post_name_abridged = mb_substr( $post_name, 0, 14 ) . '&hellip;' . mb_substr( $post_name, -14 );
 
1326
                        if ( mb_strlen( $post_name ) > 34 ) {
 
1327
                                $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
1298
1328
                        } else {
1299
1329
                                $post_name_abridged = $post_name;
1300
1330
                        }
1301
1331
                } else {
1302
 
                        if ( strlen( $post_name ) > 30 ) {
1303
 
                                $post_name_abridged = substr( $post_name, 0, 14 ) . '&hellip;' . substr( $post_name, -14 );
 
1332
                        if ( strlen( $post_name ) > 34 ) {
 
1333
                                $post_name_abridged = substr( $post_name, 0, 16 ) . '&hellip;' . substr( $post_name, -16 );
1304
1334
                        } else {
1305
1335
                                $post_name_abridged = $post_name;
1306
1336
                        }
1307
1337
                }
1308
1338
 
1309
 
                $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
 
1339
                $post_name_html = '<span id="editable-post-name">' . $post_name_abridged . '</span>';
1310
1340
                $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) );
1311
 
                $pretty_permalink = str_replace( array( '%pagename%', '%postname%' ), $post_name, urldecode( $permalink ) );
1312
1341
 
1313
 
                $return =  '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1314
 
                $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
 
1342
                $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
 
1343
                $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
1315
1344
                $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1316
 
                $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __( 'Edit' ) . "</a></span>\n";
 
1345
                $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
1317
1346
                $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
1318
1347
        }
1319
1348
 
1320
 
        if ( isset( $view_post ) ) {
1321
 
                if ( 'draft' == $post->post_status ) {
1322
 
                        $preview_link = set_url_scheme( get_permalink( $post->ID ) );
1323
 
                        /** This filter is documented in wp-admin/includes/meta-boxes.php */
1324
 
                        $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
1325
 
                        $return .= "<span id='view-post-btn'><a href='" . esc_url( $preview_link ) . "' class='button button-small' target='wp-preview-{$post->ID}'>$view_post</a></span>\n";
1326
 
                } else {
1327
 
                        if ( empty( $pretty_permalink ) ) {
1328
 
                                $pretty_permalink = $permalink;
1329
 
                        }
1330
 
 
1331
 
                        $return .= "<span id='view-post-btn'><a href='" . $pretty_permalink . "' class='button button-small'>$view_post</a></span>\n";
1332
 
                }
1333
 
        }
1334
 
 
1335
1349
        /**
1336
1350
         * Filter the sample permalink HTML markup.
1337
1351
         *
1338
1352
         * @since 2.9.0
 
1353
         * @since 4.4.0 Added `$post` parameter.
1339
1354
         *
1340
 
         * @param string      $return    Sample permalink HTML markup.
1341
 
         * @param int|WP_Post $id        Post object or ID.
1342
 
         * @param string      $new_title New sample permalink title.
1343
 
         * @param string      $new_slug  New sample permalink slug.
 
1355
         * @param string  $return    Sample permalink HTML markup.
 
1356
         * @param int     $post_id   Post ID.
 
1357
         * @param string  $new_title New sample permalink title.
 
1358
         * @param string  $new_slug  New sample permalink slug.
 
1359
         * @param WP_Post $post      Post object.
1344
1360
         */
1345
 
        $return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug );
 
1361
        $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
1346
1362
 
1347
1363
        return $return;
1348
1364
}
1374
1390
        );
1375
1391
 
1376
1392
        if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
1377
 
                $old_content_width = $content_width;
1378
 
                $content_width = 266;
1379
 
                if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
1380
 
                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
1381
 
                else
1382
 
                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
 
1393
                $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
 
1394
 
 
1395
                /**
 
1396
                 * Filter the size used to display the post thumbnail image in the 'Featured Image' meta box.
 
1397
                 *
 
1398
                 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
 
1399
                 * image size is registered, which differs from the 'thumbnail' image size
 
1400
                 * managed via the Settings > Media screen. See the `$size` parameter description
 
1401
                 * for more information on default values.
 
1402
                 *
 
1403
                 * @since 4.4.0
 
1404
                 *
 
1405
                 * @param string|array $size         Post thumbnail image size to display in the meta box. Accepts any valid
 
1406
                 *                                   image size, or an array of width and height values in pixels (in that order).
 
1407
                 *                                   If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
 
1408
                 *                                   default is an array with 266 as both the height and width values.
 
1409
                 * @param int          $thumbnail_id Post thumbnail attachment ID.
 
1410
                 * @param WP_Post      $post         The post object associated with the thumbnail.
 
1411
                 */
 
1412
                $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
 
1413
 
 
1414
                $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
 
1415
 
1383
1416
                if ( !empty( $thumbnail_html ) ) {
1384
1417
                        $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
1385
1418
                        $content = sprintf( $set_thumbnail_link,
1389
1422
                        );
1390
1423
                        $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
1391
1424
                }
1392
 
                $content_width = $old_content_width;
1393
1425
        }
1394
1426
 
1395
1427
        /**
1508
1540
        <?php
1509
1541
 
1510
1542
        if ( $locked ) {
 
1543
                $query_args = array();
1511
1544
                if ( get_post_type_object( $post->post_type )->public ) {
1512
 
                        $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
1513
 
 
1514
1545
                        if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
1515
1546
                                // Latest content is in autosave
1516
1547
                                $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
1517
 
                                $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
 
1548
                                $query_args['preview_id'] = $post->ID;
 
1549
                                $query_args['preview_nonce'] = $nonce;
1518
1550
                        }
1519
 
                } else {
1520
 
                        $preview_link = '';
1521
1551
                }
1522
1552
 
1523
 
                /** This filter is documented in wp-admin/includes/meta-boxes.php */
1524
 
                $preview_link = apply_filters( 'preview_post_link', $preview_link, $post );
 
1553
                $preview_link = get_preview_post_link( $post->ID, $query_args );
1525
1554
 
1526
1555
                /**
1527
1556
                 * Filter whether to allow the post lock to be overridden.
1582
1611
                        <div class="post-locked-avatar"></div>
1583
1612
                        <p class="wp-tab-first" tabindex="0">
1584
1613
                        <span class="currently-editing"></span><br />
1585
 
                        <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" /> <?php _e('Saving revision...'); ?></span>
 
1614
                        <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision&hellip;' ); ?></span>
1586
1615
                        <span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
1587
1616
                        </p>
1588
1617
                        <?php
1707
1736
        if ( is_wp_error( $saved_post_id ) )
1708
1737
                wp_die( $saved_post_id->get_error_message() );
1709
1738
 
1710
 
        $query_args = array( 'preview' => 'true' );
 
1739
        $query_args = array();
1711
1740
 
1712
1741
        if ( $is_autosave && $saved_post_id ) {
1713
1742
                $query_args['preview_id'] = $post->ID;
1717
1746
                        $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
1718
1747
        }
1719
1748
 
1720
 
        $url = add_query_arg( $query_args, get_permalink( $post->ID ) );
1721
 
 
1722
 
        /** This filter is documented in wp-admin/includes/meta-boxes.php */
1723
 
        return apply_filters( 'preview_post_link', $url, $post );
 
1749
        return get_preview_post_link( $post, $query_args );
1724
1750
}
1725
1751
 
1726
1752
/**
1766
1792
                return wp_create_post_autosave( wp_slash( $post_data ) );
1767
1793
        }
1768
1794
}
 
1795
 
 
1796
/**
 
1797
 * Redirect to previous page.
 
1798
 *
 
1799
 * @param int $post_id Optional. Post ID.
 
1800
 */
 
1801
function redirect_post($post_id = '') {
 
1802
        if ( isset($_POST['save']) || isset($_POST['publish']) ) {
 
1803
                $status = get_post_status( $post_id );
 
1804
 
 
1805
                if ( isset( $_POST['publish'] ) ) {
 
1806
                        switch ( $status ) {
 
1807
                                case 'pending':
 
1808
                                        $message = 8;
 
1809
                                        break;
 
1810
                                case 'future':
 
1811
                                        $message = 9;
 
1812
                                        break;
 
1813
                                default:
 
1814
                                        $message = 6;
 
1815
                        }
 
1816
                } else {
 
1817
                        $message = 'draft' == $status ? 10 : 1;
 
1818
                }
 
1819
 
 
1820
                $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
 
1821
        } elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
 
1822
                $location = add_query_arg( 'message', 2, wp_get_referer() );
 
1823
                $location = explode('#', $location);
 
1824
                $location = $location[0] . '#postcustom';
 
1825
        } elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
 
1826
                $location = add_query_arg( 'message', 3, wp_get_referer() );
 
1827
                $location = explode('#', $location);
 
1828
                $location = $location[0] . '#postcustom';
 
1829
        } else {
 
1830
                $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
 
1831
        }
 
1832
 
 
1833
        /**
 
1834
         * Filter the post redirect destination URL.
 
1835
         *
 
1836
         * @since 2.9.0
 
1837
         *
 
1838
         * @param string $location The destination URL.
 
1839
         * @param int    $post_id  The post ID.
 
1840
         */
 
1841
        wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
 
1842
        exit;
 
1843
}
 
 
b'\\ No newline at end of file'