-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-post-by-email-links.php
122 lines (107 loc) · 3.09 KB
/
class-post-by-email-links.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Post By Email Links class.
*
* @package Post_By_Email_Links
* @author Barry Ceelen <b@rryceelen.com>
* @license GPL-2.0+
* @link https://github.com/barryceelen/wp-post-by-email-links
* @copyright 2013 Barry Ceelen
*
*/
/**
* Plugin class.
*
* @package Post_By_Email_Links
* @author Barry Ceelen <b@rryceelen.com>
*/
class Post_By_Email_Links {
/**
* Instance of this class.
*
* @since 0.0.1
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin.
*
* @since 0.0.1
*/
private function __construct() {
// Maybe set the post format to 'link'
add_action( 'publish_phone', array( $this, 'maybe_set_post_format' ) );
}
/**
* Return an instance of this class.
*
* @since 0.0.1
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Set the post format to 'link' if the post content is a URL.
*
* Only processes the post if the post format has not been set before.
*
* @since 0.0.1
*
* @param int $post_id ID of the post
* @return int ID of the post
* @todo Check for allowed protocols
* @todo Sometimes mail programs create a hyperlink when inserting a URL, add check for hyperlink
*/
function maybe_set_post_format( $post_id ) {
if ( false == get_post_format( $post_id ) ) {
$content = trim( get_post_field( 'post_content', $post_id, 'raw' ) );
if ( ! strpos( $content, ' ') && $this->is_url( $content ) ) {
// Strip pesky 'utm' query vars
$content = preg_replace( "/&?utm_(.*?)\=[^&]+/", '', $content );
$content = esc_url( $content );
set_post_format( $post_id, 'link' );
// Create array for wp_update_post where the URL will be replaced by a link
$post_arr = array(
'ID' => $post_id,
'post_content' => '<a href="' . $content . '">' . $content . '</a>',
);
// Allow plugins to do stuff to the content we are about to save
$post_arr = apply_filters( 'post_by_email_links_before_update_post', $post_arr, $content );
// Update post
wp_update_post( $post_arr );
// Save original url as post_meta, in case we want it later
add_post_meta( $post_id, 'original_url', $content );
}
}
return $post_id;
}
/**
* Check if a string is a valid URL according to php's filter_var().
*
* As the FILTER_VALIDATE_URL filter doesn't seem to like internationalized domain names,
* a second pass is added which encodes the string before validation.
*
* @since 0.0.1
*
* @param string
* @return boolean|string Returns false if not a valid URL, else returns the URL
*/
function is_url( $str ) {
if ( filter_var( $str, FILTER_VALIDATE_URL ) ) {
return $str;
}
require_once( plugin_dir_path( __FILE__ ) . 'inc/idna_convert.class.php' );
$idna = new idna_convert( array( 'idn_version' => '2008' ) );
if ( filter_var( $idna->encode( $str, 'utf8' ), FILTER_VALIDATE_URL ) ) {
return $str;
}
return false;
}
}