Skip to content

Commit 5e6d391

Browse files
author
Antti Kuosmanen
committed
Conform to WP Coding standards, add Travis Linting
1 parent 658e6ae commit 5e6d391

10 files changed

Lines changed: 441 additions & 217 deletions

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
sudo: false
2+
language: php
3+
php:
4+
- 5.6
5+
- 7.1
6+
7+
cache:
8+
directories:
9+
- $HOME/.composer/cache
10+
- $HOME/.composer/vendor
11+
- vendor
12+
13+
before_install:
14+
# Update Composer
15+
- composer self-update
16+
17+
# Install phpcs with wp-coding-standards
18+
- composer create-project wp-coding-standards/wpcs:dev-master --no-dev $HOME/wpcs
19+
20+
script:
21+
# Check project with phps
22+
- ~/wpcs/vendor/bin/phpcs --extensions=php --standard=./phpcs.xml -n -p .
23+
24+
# Check that composer is valid and installs correctly
25+
- composer install -o --prefer-dist --no-interaction

classes/class-cpt-wplf-form.php

Lines changed: 139 additions & 96 deletions
Large diffs are not rendered by default.

classes/class-cpt-wplf-submission.php

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
if ( !class_exists('CPT_WPLF_Submission') ) :
3+
if ( ! class_exists( 'CPT_WPLF_Submission' ) ) :
44

55
class CPT_WPLF_Submission {
66
/**
@@ -26,15 +26,16 @@ private function __construct() {
2626
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes_cpt' ) );
2727

2828
// edit.php view
29-
add_filter( 'manage_edit-wplf-submission_columns' , array( $this, 'custom_columns_cpt' ), 100, 1);
30-
add_action( 'manage_posts_custom_column' , array( $this, 'custom_columns_display_cpt' ), 10, 2 );
29+
add_filter( 'manage_edit-wplf-submission_columns', array( $this, 'custom_columns_cpt' ), 100, 1 );
30+
add_action( 'manage_posts_custom_column', array( $this, 'custom_columns_display_cpt' ), 10, 2 );
3131
add_action( 'restrict_manage_posts', array( $this, 'form_filter_dropdown' ) );
3232
add_filter( 'pre_get_posts', array( $this, 'filter_by_form' ) );
3333

3434
// add custom bulk actions
35-
add_filter( 'bulk_actions-edit-wplf-submission', array( $this, 'register_wplf_submission_bulk_actions' ) );
36-
add_filter( 'handle_bulk_actions-edit-wplf-submission', array( $this, 'wplf_submission_bulk_action_handler' ), 10, 3 );
3735
add_action( 'admin_notices', array( $this, 'wplf_submission_bulk_action_admin_notice' ) );
36+
add_filter( 'bulk_actions-edit-wplf-submission', array( $this, 'register_wplf_submission_bulk_actions' ) );
37+
add_filter( 'handle_bulk_actions-edit-wplf-submission',
38+
array( $this, 'wplf_submission_bulk_action_handler' ), 10, 3 );
3839
}
3940

4041
public static function register_cpt() {
@@ -51,7 +52,7 @@ public static function register_cpt() {
5152
'all_items' => __( 'Submissions', 'wp-libre-form' ),
5253
'search_items' => __( 'Search Submissions', 'wp-libre-form' ),
5354
'not_found' => __( 'No submissions found.', 'wp-libre-form' ),
54-
'not_found_in_trash' => __( 'No submissions found in Trash.', 'wp-libre-form' )
55+
'not_found_in_trash' => __( 'No submissions found in Trash.', 'wp-libre-form' ),
5556
);
5657

5758
$args = array(
@@ -67,7 +68,7 @@ public static function register_cpt() {
6768
'has_archive' => false,
6869
'hierarchical' => false,
6970
'menu_position' => null,
70-
'supports' => array( 'title', 'custom-fields' )
71+
'supports' => array( 'title', 'custom-fields' ),
7172
);
7273

7374
register_post_type( 'wplf-submission', $args );
@@ -78,15 +79,17 @@ public static function register_cpt() {
7879
* Custom column display for Submission CPT in edit.php
7980
*/
8081
function custom_columns_display_cpt( $column, $post_id ) {
81-
if( 'referrer' === $column ) {
82-
if( $referrer = get_post_meta($post_id, 'referrer', true) ) {
82+
if ( 'referrer' === $column ) {
83+
if ( $referrer = get_post_meta( $post_id, 'referrer', true ) ) {
8384
echo '<a href="' . esc_url_raw( $referrer ) . '">' . esc_url( $referrer ) . '</a>';
8485
}
8586
}
86-
if( 'form' === $column ) {
87-
if( $form_id = get_post_meta($post_id, '_form_id', true) ) {
87+
if ( 'form' === $column ) {
88+
if ( $form_id = get_post_meta( $post_id, '_form_id', true ) ) {
8889
$form = get_post( $form_id );
89-
echo '<a href="' . get_edit_post_link( $form_id, '' ) . '" target="_blank">' . esc_html( $form->post_title ) . '</a>';
90+
echo '<a href="' . esc_url_raw( get_edit_post_link( $form_id, '' ) ). '" target="_blank">';
91+
echo esc_html( $form->post_title );
92+
echo '</a>';
9093
}
9194
}
9295
}
@@ -111,35 +114,36 @@ function custom_columns_cpt( $columns ) {
111114
function form_filter_dropdown() {
112115
global $pagenow;
113116

114-
$allowed = array("wplf-submission"); // show filter on these post types (currently only one?)
115-
$allowed = apply_filters("wplf-dropdown-filter", $allowed);
116-
$post_type = get_query_var("post_type");
117+
$allowed = array( 'wplf-submission' ); // show filter on these post types (currently only one?)
118+
$allowed = apply_filters( 'wplf-dropdown-filter', $allowed );
119+
$post_type = get_query_var( 'post_type' );
117120

118-
if( 'edit.php' != $pagenow || !in_array($post_type, $allowed)) {
121+
if ( 'edit.php' !== $pagenow || ! in_array( $post_type, $allowed, true ) ) {
119122
return;
120123
}
121124

122-
$transient = get_transient("wplf-form-filter");
125+
$transient = get_transient( 'wplf-form-filter' );
123126

124-
if($transient){
127+
if ( $transient ) {
125128
$forms = $transient;
126-
}
127-
128-
else{
129+
} else {
129130
$forms = get_posts( array(
130131
'post_per_page' => '-1',
131132
'post_type' => 'wplf-form',
132133
) );
133134

134-
set_transient("wplf-form-filter", $forms, 15 * MINUTE_IN_SECONDS);
135+
set_transient( 'wplf-form-filter', $forms, 15 * MINUTE_IN_SECONDS );
135136
}
136137

137138
?>
138139
<label for="filter-by-form" class="screen-reader-text">Filter by form</label>
139140
<select name="form" id="filter-by-form">
140-
<option value="0"><?php _e('All Forms', 'wp-libre-form'); ?></option>
141-
<?php foreach( $forms as $form ) : ?>
142-
<option value="<?php echo $form->ID; ?>" <?php echo isset( $_REQUEST['form'] ) && $_REQUEST['form'] == $form->ID ? 'selected' : ''; ?>><?php echo $form->post_title; ?></option>
141+
<option value="0"><?php esc_html_e( 'All Forms', 'wp-libre-form' ); ?></option>
142+
<?php foreach ( $forms as $form ) : ?>
143+
<option
144+
value="<?php echo intval( $form->ID ); ?>"
145+
<?php echo isset( $_REQUEST['form'] ) && intval( $_REQUEST['form'] ) === $form->ID ? 'selected' : ''; ?>
146+
><?php esc_html( $form->post_title ); ?></option>
143147
<?php endforeach; ?>
144148
</select>
145149
<?php
@@ -151,15 +155,15 @@ function form_filter_dropdown() {
151155
function filter_by_form( $query ) {
152156
global $pagenow;
153157

154-
if( 'edit.php' != $pagenow ) {
158+
if ( 'edit.php' !== $pagenow ) {
155159
return $query;
156160
}
157161

158-
if( $query->get( 'post_type' ) != 'wplf-submission' ) {
162+
if ( $query->get( 'post_type' ) !== 'wplf-submission' ) {
159163
return $query;
160164
}
161165

162-
if( isset( $_REQUEST['form'] ) && ! empty( $_REQUEST['form'] ) ) {
166+
if ( isset( $_REQUEST['form'] ) && ! empty( $_REQUEST['form'] ) ) {
163167
$query->set( 'meta_key', '_form_id' );
164168
$query->set( 'meta_value', intval( $_REQUEST['form'] ) );
165169
}
@@ -168,16 +172,16 @@ function filter_by_form( $query ) {
168172
}
169173

170174
function register_wplf_submission_bulk_actions( $bulk_actions ) {
171-
$bulk_actions['wplf_resend_copy'] = __('Resend email copy', 'wp-libre-form');
175+
$bulk_actions['wplf_resend_copy'] = __( 'Resend email copy', 'wp-libre-form' );
172176
return $bulk_actions;
173177
}
174178

175179
function wplf_submission_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
176-
if( $doaction !== 'wplf_resend_copy' ) {
180+
if ( $doaction !== 'wplf_resend_copy' ) {
177181
return $redirect_to;
178182
}
179183

180-
foreach( $post_ids as $post_id ) {
184+
foreach ( $post_ids as $post_id ) {
181185
$return = new stdClass();
182186
$return->ok = 1;
183187

@@ -189,14 +193,18 @@ function wplf_submission_bulk_action_handler( $redirect_to, $doaction, $post_ids
189193
}
190194

191195
function wplf_submission_bulk_action_admin_notice() {
192-
if( !empty( $_REQUEST['wplf_resent'] ) ) {
196+
if ( ! empty( $_REQUEST['wplf_resent'] ) ) {
193197
$count = intval( $_REQUEST['wplf_resent'] );
194-
printf( '<div id="wplf-submission-bulk-resend-message" class="notice notice-success"><p>' .
195-
_n( 'Resent email copy of %s submission.',
196-
'Resent email copy of %s submissions.',
197-
$count,
198-
'wp-libre-form'
199-
) . '</p></div>', $count );
198+
printf(
199+
'<div id="wplf-submission-bulk-resend-message" class="notice notice-success"><p>' .
200+
esc_html__(
201+
_n( 'Resent email copy of %s submission.',
202+
'Resent email copy of %s submissions.',
203+
$count ),
204+
'wp-libre-form'
205+
) . '</p></div>',
206+
intval( $count )
207+
);
200208
}
201209
}
202210

@@ -228,13 +236,13 @@ function metabox_submission() {
228236
<table class="wp-list-table widefat striped">
229237
<thead>
230238
<tr>
231-
<th><strong><?php _e( 'Field', 'wp-libre-form' ); ?></strong></th>
232-
<th><strong><?php _e( 'Value', 'wp-libre-form' ); ?></strong></th>
239+
<th><strong><?php esc_html_e( 'Field', 'wp-libre-form' ); ?></strong></th>
240+
<th><strong><?php esc_html_e( 'Value', 'wp-libre-form' ); ?></strong></th>
233241
</tr>
234242
</thead>
235243
<tbody>
236-
<?php foreach( $fields as $field ) : ?>
237-
<?php if( '_' != $field[0] ) : ?>
244+
<?php foreach ( $fields as $field ) : ?>
245+
<?php if ( '_' !== $field[0] ) : ?>
238246
<?php
239247
$value = $postmeta[ $field ][0];
240248

@@ -243,9 +251,12 @@ function metabox_submission() {
243251

244252
// if the field ends with '_attachment' and there is an attachment url that corresponds to the id, show a link
245253
$attachment_suffix = '_attachment';
246-
if ( substr( $field, -strlen( $attachment_suffix ) ) === $attachment_suffix && wp_get_attachment_url( $value ) ) {
247-
$link_text = __( 'View Attachment', 'wp-libre-form' );
248-
$possible_link = '<a target="_blank" href="' . get_edit_post_link( $value ) . '" style="float:right">' . $link_text . '</a>';
254+
if ( substr( $field, -strlen( $attachment_suffix ) ) === $attachment_suffix ) {
255+
if ( wp_get_attachment_url( $value ) ) {
256+
$link_text = __( 'View Attachment', 'wp-libre-form' );
257+
$possible_link = '<a target="_blank" href="' . get_edit_post_link( $value ) . '" style="float:right">';
258+
$possible_link .= $link_text . '</a>';
259+
}
249260
}
250261

251262
// Show a link if the field corresponds to a URL
@@ -257,8 +268,8 @@ function metabox_submission() {
257268
}
258269
?>
259270
<tr>
260-
<th><strong><?php echo $field; ?></strong> <?php echo $possible_link; ?></th>
261-
<?php if( strlen( $value ) > 60 || strpos( $value, "\n" ) ) : ?>
271+
<th><strong><?php echo esc_html( $field ); ?></strong> <?php echo wp_kses( $possible_link ); ?></th>
272+
<?php if ( strlen( $value ) > 60 || strpos( $value, "\n" ) ) : ?>
262273
<td><textarea style="width:100%" readonly><?php echo esc_textarea( $value ); ?></textarea></td>
263274
<?php else : ?>
264275
<td><input style="width:100%" type="text" value="<?php esc_attr_e( $value ); ?>" readonly></td>

classes/class-wplf-polylang.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
if ( !class_exists( 'WPLF_Polylang' ) ) {
2+
if ( ! class_exists( 'WPLF_Polylang' ) ) {
33
class WPLF_Polylang {
44
/**
55
* CPT for the forms
@@ -20,55 +20,55 @@ public static function init() {
2020
*/
2121
private function __construct() {
2222
add_filter( 'wplf_form', array( $this, 'render_form' ) );
23-
add_filter( 'save_post_wplf-form', array( $this, 'save_form' ), 10, 3);
24-
add_action( 'after_setup_theme', array ( $this, 'register_strings') );
23+
add_filter( 'save_post_wplf-form', array( $this, 'save_form' ), 10, 3 );
24+
add_action( 'after_setup_theme', array( $this, 'register_strings' ) );
2525

2626
$this->strings = get_option( 'wplf-translation-strings', array() );
2727
$this->register_strings();
2828
}
2929

30-
public function render_form ( $form_content ) {
30+
public function render_form( $form_content ) {
3131
// Get all strings inside double curly braces.
3232
preg_match_all( $this->regular_expression, $form_content, $matches );
33-
foreach( $matches[0] as $match ){
33+
foreach ( $matches[0] as $match ) {
3434
// match contains the braces, get rid of them.
35-
$string = trim( str_replace( array( "{" , "}" ), array( "", "" ), $match ) );
36-
$form_content = str_replace( $match, $this->translate_string ( $string ), $form_content );
35+
$string = trim( str_replace( array( '{', '}' ), array( '', '' ), $match ) );
36+
$form_content = str_replace( $match, $this->translate_string( $string ), $form_content );
3737
}
3838

3939
return $form_content;
4040
}
4141

42-
function save_form ( $post_id, $post, $update ) {
42+
function save_form( $post_id, $post, $update ) {
43+
unset( $post_id, $update ); // not used here so shut up linter!
44+
4345
preg_match_all( $this->regular_expression, $post->post_content, $matches );
44-
if( !empty( $matches ) ) {
45-
foreach( $matches[0] as $match ){
46+
if ( ! empty( $matches ) ) {
47+
foreach ( $matches[0] as $match ) {
4648
// match contains the braces, get rid of them.
47-
$string = trim( str_replace( array( "{" , "}" ), array( "", "" ), $match ) );
48-
$this->strings[$string] = null;
49+
$string = trim( str_replace( array( '{', '}' ), array( '', '' ), $match ) );
50+
$this->strings[ $string ] = null;
4951
// By storing the string as the array key, we don't need to use array_unique.
5052
}
5153
}
5254

5355
update_option( 'wplf-translation-strings', $this->strings ); // Let's be optimistic.
5456
}
5557

56-
public function register_strings () {
57-
foreach( $this->strings as $string => $value ) {
58+
public function register_strings() {
59+
foreach ( $this->strings as $string => $value ) {
5860
$this->register_string( $string );
5961
}
6062
}
6163

62-
public function register_string ( $string ) {
63-
if( function_exists( 'pll_register_string' ) ) {
64+
public function register_string( $string ) {
65+
if ( function_exists( 'pll_register_string' ) ) {
6466
pll_register_string( $string, $string, 'WP Libre Form' );
65-
} else {
66-
// Don't kill anything.
6767
}
6868
}
6969

70-
public function translate_string ( $string ) {
71-
if( function_exists( 'pll__' ) ) {
70+
public function translate_string( $string ) {
71+
if ( function_exists( 'pll__' ) ) {
7272
return pll__( $string );
7373
} else {
7474
return $string; // Don't kill anything.

composer.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)