1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Plugin Helpers. |
4
|
|
|
* |
5
|
|
|
* @package WP_To_Diaspora\Helpers |
6
|
|
|
* @since 1.3.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
// Exit if accessed directly. |
10
|
|
|
defined( 'ABSPATH' ) || exit; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Various helper methods. |
14
|
|
|
*/ |
15
|
|
|
class WP2D_Helpers { |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Debug text that get's accumulated before output. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $_debugging = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add a line to the debug output. Include the stack trace to see where it's coming from. |
26
|
|
|
* |
27
|
|
|
* @param string $text Text to add. |
28
|
|
|
*/ |
29
|
|
|
public static function add_debugging( $text ) { |
30
|
|
|
// Make sure we're in debug mode. |
31
|
|
|
if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) { |
32
|
|
|
$d = ''; |
33
|
|
|
foreach ( debug_backtrace() as $dbt ) { |
34
|
|
|
extract( $dbt ); |
35
|
|
|
// Only trace back as far as the plugin goes. |
36
|
|
|
if ( strstr( $file, plugin_dir_path( dirname( __FILE__ ) ) ) ) { |
37
|
|
|
$d = sprintf( "%s%s%s [%s:%s]\n", $class, $type, $function, basename( $file ), $line ) . $d; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
self::$_debugging .= sprintf( "%s\n%s\n", date( 'Y.m.d H:i:s' ), $d . $text ); |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return the debug output. |
50
|
|
|
* |
51
|
|
|
* @return string The debug output. |
52
|
|
|
*/ |
53
|
|
|
public static function get_debugging() { |
54
|
|
|
if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) { |
55
|
|
|
return self::$_debugging; |
56
|
|
|
} |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert a string with comma seperated values to an array. |
62
|
|
|
* |
63
|
|
|
* @todo Make $input by value. |
64
|
|
|
* |
65
|
|
|
* @param array|string $input The string to be converted. |
66
|
|
|
* @return array The converted array. |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public static function str_to_arr( &$input ) { |
|
|
|
|
69
|
|
|
if ( ! is_array( $input ) ) { |
70
|
|
|
// Explode string > Trim each entry > Remove blanks > Re-index array. |
71
|
|
|
$input = array_values( array_filter( array_map( 'trim', explode( ',', $input ) ) ) ); |
72
|
|
|
} else { |
73
|
|
|
// If we're already an array, make sure we return it clean. |
74
|
|
|
self::arr_to_str( $input ); |
75
|
|
|
self::str_to_arr( $input ); |
76
|
|
|
} |
77
|
|
|
return $input; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert an array to a string with comma seperated values. |
82
|
|
|
* |
83
|
|
|
* @todo Make $input by value. |
84
|
|
|
* |
85
|
|
|
* @param array|string $input The array to be converted. |
86
|
|
|
* @return string The converted string. |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public static function arr_to_str( &$input ) { |
|
|
|
|
89
|
|
|
if ( is_array( $input ) ) { |
90
|
|
|
// Trim each entry > Remove blanks > Implode them together. |
91
|
|
|
$input = implode( ',', array_filter( array_map( 'trim', $input ) ) ); |
92
|
|
|
} else { |
93
|
|
|
// If we're already a string, make sure we return it clean. |
94
|
|
|
self::str_to_arr( $input ); |
95
|
|
|
self::arr_to_str( $input ); |
96
|
|
|
} |
97
|
|
|
return $input; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Encrypt the passed string with the passed key. |
102
|
|
|
* |
103
|
|
|
* @param string $input String to be encrypted. |
104
|
|
|
* @param string $key The key used for the encryption. |
105
|
|
|
* @return string The encrypted string. |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public static function encrypt( $input, $key = AUTH_KEY ) { |
|
|
|
|
108
|
|
|
if ( is_null( $input ) || '' === $input ) { |
109
|
|
|
return false; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
global $wpdb; |
|
|
|
|
112
|
|
|
return $wpdb->get_var( $wpdb->prepare( 'SELECT HEX(AES_ENCRYPT(%s,%s))', $input, $key ) ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Decrypt the passed string with the passed key. |
117
|
|
|
* |
118
|
|
|
* @param string $input String to be decrypted. |
119
|
|
|
* @param string $key The key used for the decryption. |
120
|
|
|
* @return string The decrypted string. |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public static function decrypt( $input, $key = AUTH_KEY ) { |
|
|
|
|
123
|
|
|
if ( is_null( $input ) || '' === $input ) { |
124
|
|
|
return false; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
global $wpdb; |
|
|
|
|
127
|
|
|
return $wpdb->get_var( $wpdb->prepare( 'SELECT AES_DECRYPT(UNHEX(%s),%s)', $input, $key ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set up and return an API connection using the currently saved options.. |
132
|
|
|
* |
133
|
|
|
* @return WP2D_API The API object. |
134
|
|
|
*/ |
135
|
|
|
public static function api_quick_connect() { |
136
|
|
|
$options = WP2D_Options::instance(); |
137
|
|
|
$pod = (string) $options->get_option( 'pod' ); |
138
|
|
|
$is_secure = true; |
139
|
|
|
$username = (string) $options->get_option( 'username' ); |
140
|
|
|
$password = WP2D_Helpers::decrypt( (string) $options->get_option( 'password' ) ); |
141
|
|
|
|
142
|
|
|
$api = new WP2D_API( $pod, $is_secure ); |
143
|
|
|
|
144
|
|
|
// This is necessary for correct error handling! |
145
|
|
|
if ( $api->init() ) { |
146
|
|
|
$api->login( $username, $password ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( $api->has_last_error() ) { |
150
|
|
|
self::add_debugging( $api->get_last_error() ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $api; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.