Skip to content

Introduce phpcs and WP coding standard #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ matrix:
env: WP_VERSION=latest
# - php: 5.6
# env: WP_VERSION=trunk
# - php: 5.6
# env: WP_TRAVISCI=phpcs
- php: 5.6
env: WP_TRAVISCI=phpcs
# - php: 5.3
# env: WP_VERSION=latest
# dist: precise
Expand Down
184 changes: 184 additions & 0 deletions includes/class-customfieldspermalink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php
/**
* Class CustomFieldsPermalink
*
* @package WordPress_Custom_Fields_Permalink
*/

/**
* Class CustomFieldsPermalink provides the implementation of custom post fields in permalinks.
*/
class CustomFieldsPermalink {

const PARAM_CUSTOMFIELD_KEY = 'custom_field_key';
const PARAM_CUSTOMFIELD_VALUE = 'custom_field_value';

/**
* Do check against meta value or not.
*
* @var bool
*/
private static $check_custom_field_value = false;

/**
* Filters the permalink structure for a post before token replacement occurs..
* The pre_post_link filter implementation.
*
* @param string $permalink The site's permalink structure.
* @param WP_Post $post The post in question.
* @param bool $leavename Whether to keep the post name.
*
* @link https://developer.wordpress.org/reference/hooks/pre_post_link/
*
* @return mixed
*/
public static function link_post( $permalink, $post, $leavename ) {
return self::link_rewrite_fields( $permalink, $post );
}

/**
* Filters the permalink for a post of a custom post type.
* The post_type_link filter implementation.
*
* @param string $permalink The post's permalink.
* @param WP_Post $post The post in question.
* @param bool $leavename Whether to keep the post name.
* @param bool $sample Is it a sample permalink.
*
* @link https://developer.wordpress.org/reference/hooks/post_type_link/
*
* @return mixed
*/
public static function link_post_type( $permalink, $post, $leavename, $sample ) {
return self::link_rewrite_fields( $permalink, $post );
}

/**
* Rewrites permalink replacing custom fields.
*
* @param string $permalink The permalink.
* @param WP_Post $post The post.
*
* @return string
*/
private static function link_rewrite_fields( $permalink, $post ) {
$replace_callback = function ( $matches ) use ( &$post ) {
return CustomFieldsPermalink::link_rewrite_fields_extract( $post, $matches[2] );
};

return preg_replace_callback( '#(%field_(.*?)%)#', $replace_callback, $permalink );
}

/**
* Extract the metadata value from the post.
*
* @param WP_Post $post The post.
* @param string $field_name The metadata key to extract.
*
* @return string
*/
private static function link_rewrite_fields_extract( $post, $field_name ) {
$post_meta = get_post_meta( $post->ID );

if ( ! isset( $post_meta[ $field_name ] ) ) {
return '';
}

$value = $post_meta[ $field_name ][0];

$value = sanitize_title( $value );

return $value;
}

/**
* Filters the query variables whitelist before processing.
* The query_vars filter implementation.
*
* @param array $public_query_vars The array of whitelisted query variables.
*
* @link https://developer.wordpress.org/reference/hooks/query_vars/
*
* @return mixed
*/
public static function register_extra_query_vars( $public_query_vars ) {
array_push( $public_query_vars, self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE );

return $public_query_vars;
}

/**
* Filters the array of parsed query variables.
* The request filter implementation.
*
* @param array $query_vars The array of requested query variables.
*
* @link https://developer.wordpress.org/reference/hooks/request/
*
* @return mixed
*/
public static function process_request( $query_vars ) {
// Additional parameters added to WordPress.
// Main Loop query.
if ( array_key_exists( self::PARAM_CUSTOMFIELD_KEY, $query_vars ) ) {
$query_vars['meta_key'] = $query_vars[ self::PARAM_CUSTOMFIELD_KEY ];

// Remove temporary injected parameter.
unset( $query_vars[ self::PARAM_CUSTOMFIELD_KEY ] );

// Do not check field's value for this moment.
if ( true === self::$check_custom_field_value ) {
if ( array_key_exists( self::PARAM_CUSTOMFIELD_VALUE, $query_vars ) ) {
$query_vars['meta_value'] = $query_vars[ self::PARAM_CUSTOMFIELD_VALUE ];

// Remove temporary injected parameter.
unset( $query_vars[ self::PARAM_CUSTOMFIELD_VALUE ] );
}
}
}

return $query_vars;
}

/**
* Filters the full set of generated rewrite rules.
* The rewrite_rules_array filter implementation.
*
* @param array $rules The compiled array of rewrite rules.
*
* @link https://developer.wordpress.org/reference/hooks/rewrite_rules_array/
*
* @return array
*/
public static function rewrite_rules_array_filter( $rules ) {
$keys = array_keys( $rules );
$tmp = $rules;
$rules = array();

$j = sizeof( $keys );
for ( $i = 0; $i < $j; ++ $i ) {
$key = $keys[ $i ];

if ( preg_match( '/%field_([^%]*?)%/', $key ) ) {
$key_new = preg_replace(
'/%field_([^%]*?)%/',
'([^/]+)',
// You can simply add next group to the url, because WordPress.
// Detect them automatically and add next $matches indices.
$key
);
$rules[ $key_new ] = preg_replace(
'/%field_([^%]*?)%/',
sprintf( '%s=$1&%s=', self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE ),
// Here on the end will be pasted $matches[$i] from $keyNew,
// so we can grab it it the future in self::PARAM_CUSTOMFIELD_VALUE parameter.
$tmp[ $key ]
);
} else {
$rules[ $key ] = $tmp[ $key ];
}
}

return $rules;
}
}
17 changes: 17 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
<description>Generally-applicable sniffs for WordPress plugins</description>

<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />

<!-- Check all PHP files in directory tree by default. -->
<arg name="extensions" value="php"/>
<file>.</file>

<!-- Show progress and sniff codes in all reports -->
<arg value="ps"/>

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
</ruleset>
16 changes: 0 additions & 16 deletions test/PermalinkAsserter.php

This file was deleted.

19 changes: 0 additions & 19 deletions test/PermalinkSteps.php

This file was deleted.

9 changes: 7 additions & 2 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Tests bootstrap file.
*
* @package WordPress_Custom_Fields_Permalink
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );

Expand All @@ -25,5 +30,5 @@ function _manually_load_plugin() {
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

require 'PermalinkSteps.php';
require 'PermalinkAsserter.php';
require 'class-permalinksteps.php';
require 'class-permalinkasserter.php';
40 changes: 40 additions & 0 deletions test/class-permalinkasserter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Tests util file.
*
* @package WordPress_Custom_Fields_Permalink
*/

/**
* Class PermalinkAsserter contains utility methods to assert conditions related to permalinks.
*/
class PermalinkAsserter {

/**
* Parent unit test case.
*
* @var WP_UnitTestCase
*/
private $unit_test_case;

/**
* PermalinkAsserter constructor.
*
* @param WP_UnitTestCase $unit_test_case Parent unit test case.
*/
public function __construct( WP_UnitTestCase $unit_test_case ) {
$this->unit_test_case = $unit_test_case;
}

/**
* Checks if post has an expected permalink.
*
* @param WP_Post $post The post.
* @param string $permalink Expected permalink.
*/
public function has_permalink( $post, $permalink ) {
$actual = wp_make_link_relative( get_the_permalink( $post ) );

$this->unit_test_case->assertEquals( $permalink, $actual );
}
}
38 changes: 38 additions & 0 deletions test/class-permalinksteps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Tests util file.
*
* @package WordPress_Custom_Fields_Permalink
*/

/**
* Class PermalinkSteps contains utility methods to arrange conditions related to permalinks.
*/
class PermalinkSteps {

/**
* PermalinkSteps constructor.
*/
public function __construct() {
}

/**
* Sets the given permalink structure.
*
* @param string $structure The permalink structure.
*/
public function given_permalink_structure( $structure ) {
global $wp_rewrite;

$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( $structure );
$wp_rewrite->flush_rules();
}

/**
* Sets the "/%postname%/" permalink structure.
*/
public function given_postname_permalink_structure() {
$this->given_permalink_structure( '/%postname%/' );
}
}
Loading