-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp-article-order.php
98 lines (79 loc) · 2.13 KB
/
wp-article-order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* Plugin Name: WP Article Order
* Plugin URI: http://wordpress.org/plugins/wp-article-order/
* Author: John James Jacoby
* Author URI: https://profiles.wordpress.org/johnjamesjacoby/
* Version: 0.1.1
* Description: Move articles to the end of post titles
* License: GPL v2 or later
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Look for articles in post titles and move them to the end
*
* @todo Every other language, prefix vs. suffix, etc...
* @since 0.1.0
*
* @param array $data
*/
function wp_article_order_save_post( $data = '' ) {
// Bail if no title
if ( empty( $data['post_title'] ) ) {
return $data;
}
// Short circuit
if ( ! apply_filters( 'wp_article_order_pre_save_post', $data ) ) {
return $data;
}
// Get articles
$lang = get_site_option( 'WPLANG', 'en_US' );
$articles = wp_article_order_get_articles( $lang );
// Explode words into an array
$words = explode( ' ', $data['post_title'] );
// Look for leading article
if ( in_array( strtolower( $words[0] ), $articles ) ) {
// Save the first word
$first = $words[0];
// Add a comma to the last word
$words[ count( $words ) - 1 ] .= ',';
// Unset the first word
unset( $words[0] );
// Append the first word
$words[] = $first;
// Implode them back together
$data['post_title'] = implode( ' ', $words );
}
// Return maybe modified data
return $data;
}
add_filter( 'wp_insert_post_data', 'wp_article_order_save_post' );
add_filter( 'wp_insert_attachment_data', 'wp_article_order_save_post' );
/**
* Return array of articles
*
* @todo Every other language
*
* @since 0.1.0
*
* @return array
*/
function wp_article_order_get_articles( $locale = 'en_US' ) {
switch ( $locale ) {
// German
case 'de_DE' :
case 'de_AT' :
case 'de_CH' :
case 'de_LI' :
case 'de_LU' :
$retval = array( 'der', 'die', 'das', 'dem', 'den', 'des', 'ein', 'eine', 'einem', 'einen', 'einer', 'eines' );
break;
// English
case 'en_US' :
default :
$retval = array( 'the', 'a', 'an', 'some' );
break;
}
return apply_filters( 'wp_article_order_get_articles', $retval, $locale );
}