Skip to content
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

PHPCS Fixes #160

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="syndication"/>
<element value="push_syn"/>
<element value="pull_syn"/>
<element value="syn"/>
</property>
</properties>
</rule>

<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array">
<element value="syndication"/>
<element value="push-syndication"/>
</property>
</properties>
</rule>
Expand Down
132 changes: 81 additions & 51 deletions includes/class-syndication-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
*/
class Syndication_Logger_Admin_Notice {

private static $notice_option = 'syn_notices';
private static $notice_option = 'syn_notices';
private static $notice_bundles_option = 'syn_notice_bundles';

private static $dismiss_parameter = 'syn_dismiss';

/**
* Syndication_Logger_Admin_Notice constructor.
*/
public function __construct() {
add_action( 'admin_init', array( __CLASS__, 'handle_dismiss_syndication_notice' ) );
add_action( 'admin_notices', array( __CLASS__, 'display_valid_notices' ) );
}

/**
* Create a admin notice
* @param text $message_text The message you would like to show
* @param string $message_type A message type, shown alongside with your message and used to categorize and bundle messages of the same type
* @param string $class css class applied to the notice eg. 'updated', 'error', 'update-nag'
* @param boolean $summarize_multiple setting this to true allows summarizing messages of the same message_type into one message. The message text is then passed through the syn_message_text_multiple filter and all messages of this type can be dismissed at once
*
* @param string $message_text The message you would like to show.
* @param string $message_type A message type, shown alongside with your message and used to categorize and bundle messages of the same type.
* @param string $class css class applied to the notice eg. 'updated', 'error', 'update-nag'.
* @param boolean $summarize_multiple setting this to true allows summarizing messages of the same message_type into one message. The message text is then passed through the syn_message_text_multiple filter and all messages of this type can be dismissed at once.
*/
public static function add_notice( $message_text, $message_type = 'Syndication', $class = 'updated', $summarize_multiple = false ) {
$notices = get_option( self::$notice_option );

$changed = false;
$changed = false;
$message_key = md5( $message_type . $message_text );
if ( ! is_array( $notices ) || ! isset( $notices[$message_type] ) || ! isset( $notices[$message_type][$message_key] ) ) {
$notices[$message_type][$message_key] = array(
'message_text' => sanitize_text_field( $message_text ),
'summarize_multiple' => (boolean) $summarize_multiple,
'message_type' => sanitize_text_field( $message_type ),
'class' => sanitize_text_field( $class ),
if ( ! is_array( $notices ) || ! isset( $notices[ $message_type ] ) || ! isset( $notices[ $message_type ][ $message_key ] ) ) {
$notices[ $message_type ][ $message_key ] = array(
'message_text' => sanitize_text_field( $message_text ),
'summarize_multiple' => (bool) $summarize_multiple,
'message_type' => sanitize_text_field( $message_type ),
'class' => sanitize_text_field( $class ),
);
$changed = true;
$changed = true;
}

if ( true === $changed ) {
Expand All @@ -50,40 +54,40 @@ public static function add_notice( $message_text, $message_type = 'Syndication',
public static function display_valid_notices() {
$capability = apply_filters( 'syn_syndicate_cap', 'manage_options' );

$messages = get_option( self::$notice_option );
$notice_bundles = get_option( self::$notice_bundles_option );
$messages_to_display = array();
$messages = get_option( self::$notice_option );
$notice_bundles = get_option( self::$notice_bundles_option );
$messages_to_display = array();
$notice_bundles_changed = false;

if ( !is_array( $messages ) || empty( $messages ) ) {
if ( ! is_array( $messages ) || empty( $messages ) ) {
return;
}

foreach( $messages as $message_type => $message_values ) {
foreach( $message_values as $message_key => $message_data ) {
foreach ( $messages as $message_type => $message_values ) {
foreach ( $message_values as $message_key => $message_data ) {
if ( isset( $message_data['summarize_multiple'] ) && true === $message_data['summarize_multiple'] ) {
$message_text = apply_filters( 'syn_message_text_multiple', $message_data['message_text'], $message_data );
} else {
$message_text = apply_filters( 'syn_message_text', $message_data['message_text'], $message_data );
}

$new_message_key = md5( $message_type . $message_text );
$new_message_key = md5( $message_type . $message_text );
$new_message_data = array(
'message_text' => sanitize_text_field( $message_text ),
'summarize_multiple' => (boolean) $message_data['summarize_multiple'],
'message_type' => sanitize_text_field( $message_data['message_type'] ),
'class' => sanitize_text_field( $message_data['class'] )
'message_text' => sanitize_text_field( $message_text ),
'summarize_multiple' => (bool) $message_data['summarize_multiple'],
'message_type' => sanitize_text_field( $message_data['message_type'] ),
'class' => sanitize_text_field( $message_data['class'] ),
);

if ( $new_message_key != $message_key ) {
if ( ! isset( $notice_bundles[$new_message_key] ) || ! in_array( $message_key, $notice_bundles[$new_message_key] ) ) {
$notice_bundles[$new_message_key][] = $message_key;
$notice_bundles_changed = true;
if ( ! isset( $notice_bundles[ $new_message_key ] ) || ! in_array( $message_key, $notice_bundles[ $new_message_key ] ) ) {
$notice_bundles[ $new_message_key ][] = $message_key;
$notice_bundles_changed = true;
}
}

if ( current_user_can( $capability ) ) {
$messages_to_display[$message_type][$new_message_key] = $new_message_data;
$messages_to_display[ $message_type ][ $new_message_key ] = $new_message_data;
}
}
}
Expand All @@ -92,11 +96,23 @@ public static function display_valid_notices() {
update_option( self::$notice_bundles_option, $notice_bundles );
}

foreach( $messages_to_display as $message_type => $message_values ) {
foreach( $message_values as $message_key => $message_data ) {
foreach ( $messages_to_display as $message_type => $message_values ) {
foreach ( $message_values as $message_key => $message_data ) {
$dismiss_nonce = wp_create_nonce( esc_attr( $message_key ) );
printf( '<div class="%s"><p>', esc_attr( $message_data['class'] ) );
printf( __('%1$s : %2$s <a href="%3$s">Hide Notice</a>'), esc_html( $message_type ), wp_kses_post( $message_data['message_text'] ), add_query_arg( array( self::$dismiss_parameter => esc_attr( $message_key ), 'syn_dismiss_nonce' => esc_attr( $dismiss_nonce ) ) ) );
printf(
__( '%1$s : %2$s <a href="%3$s">Hide Notice</a>' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html( $message_type ),
wp_kses_post( $message_data['message_text'] ),
esc_url(
add_query_arg(
array(
self::$dismiss_parameter => esc_attr( $message_key ),
'syn_dismiss_nonce' => esc_attr( $dismiss_nonce ),
)
)
)
);
printf( '</p></div>' );
}
}
Expand All @@ -108,49 +124,63 @@ public static function display_valid_notices() {
public static function handle_dismiss_syndication_notice() {
$capability = apply_filters( 'syn_syndicate_cap', 'manage_options' );

// add nonce
if ( isset( $_GET[self::$dismiss_parameter] ) && current_user_can( $capability ) ) {

$dismiss_key = esc_attr( $_GET[self::$dismiss_parameter] );
$dismiss_nonce = esc_attr( $_GET['syn_dismiss_nonce'] );
// add nonce.
if ( isset( $_GET[ self::$dismiss_parameter ] ) && current_user_can( $capability ) ) {
$dismiss_key = esc_attr( $_GET[ self::$dismiss_parameter ] ); // phpcs:ignore
$dismiss_nonce = esc_attr( isset( $_GET['syn_dismiss_nonce'] ) ? $_GET['syn_dismiss_nonce'] : '' ); // phpcs:ignore
if ( ! wp_verify_nonce( $dismiss_nonce, $dismiss_key ) ) {
wp_die( __( "Invalid security check" ) );
wp_die( esc_html__( 'Invalid security check' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
$messages = get_option( self::$notice_option );
$messages = get_option( self::$notice_option );
$notice_bundles = get_option( self::$notice_bundles_option );

$dismiss_items = array();
if ( isset( $notice_bundles[$dismiss_key] ) ) {
$dismiss_items = $notice_bundles[$dismiss_key];
if ( isset( $notice_bundles[ $dismiss_key ] ) ) {
$dismiss_items = $notice_bundles[ $dismiss_key ];
} else {
$dismiss_items = array( $dismiss_key );
}

foreach( $messages as $message_type => $message_values ) {
foreach ( $messages as $message_type => $message_values ) {
$message_keys = array_keys( $message_values );
$dismiss_it = array_intersect( $message_keys, $dismiss_items );
foreach( $dismiss_it as $dismiss_it_key ) {
unset( $messages[$message_type][$dismiss_it_key] );
$dismiss_it = array_intersect( $message_keys, $dismiss_items );
foreach ( $dismiss_it as $dismiss_it_key ) {
unset( $messages[ $message_type ][ $dismiss_it_key ] );
}
}

if ( isset( $notice_bundles[$dismiss_key] ) ) {
unset( $notice_bundles[$dismiss_key] );
if ( isset( $notice_bundles[ $dismiss_key ] ) ) {
unset( $notice_bundles[ $dismiss_key ] );
}

update_option( self::$notice_option, $messages );
update_option( self::$notice_bundles_option, $notice_bundles );

}
}
}

add_filter( 'syn_message_text_multiple', 'syn_handle_multiple_error_notices', 10, 2 );
/**
* @param $message
* @param $message_data
*
* @return string|void
*/
function syn_handle_multiple_error_notices( $message, $message_data ) {
return __( 'There have been multiple errors. Please validate your syndication logs' );
return esc_html__( 'There have been multiple errors. Please validate your syndication logs', 'push-syndication' );
}
add_filter( 'syn_message_text_multiple', 'syn_handle_multiple_error_notices', 10, 2 );

add_action( 'push_syndication_site_disabled', 'syn_add_site_disabled_notice', 10, 2 );
/**
* @param $site_id
* @param $count
*/
function syn_add_site_disabled_notice( $site_id, $count ) {
Syndication_Logger_Admin_Notice::add_notice( $message_text = sprintf( __( 'Site %d disabled after %d pull failure(s).', 'push-syndication' ), (int) $site_id, (int) $count ), $message_type = 'Syndication site disabled', $class = 'error', $summarize_multiple = false );
Syndication_Logger_Admin_Notice::add_notice(
sprintf( __( 'Site %1$d disabled after %2$d pull failure(s).', 'push-syndication' ), (int) $site_id, (int) $count ),
'Syndication site disabled',
'error',
false
);
}
add_action( 'push_syndication_site_disabled', 'syn_add_site_disabled_notice', 10, 2 );

63 changes: 43 additions & 20 deletions includes/class-syndication-client-factory.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,68 @@
<?php

include_once( dirname( __FILE__ ) . '/class-syndication-wp-xmlrpc-client.php' );
include_once( dirname( __FILE__ ) . '/class-syndication-wp-xml-client.php' );
include_once( dirname( __FILE__ ) . '/class-syndication-wp-rest-client.php' );
include_once( dirname( __FILE__ ) . '/class-syndication-wp-rss-client.php' );

require_once dirname( __FILE__ ) . '/class-syndication-wp-xmlrpc-client.php';
require_once dirname( __FILE__ ) . '/class-syndication-wp-xml-client.php';
require_once dirname( __FILE__ ) . '/class-syndication-wp-rest-client.php';
require_once dirname( __FILE__ ) . '/class-syndication-wp-rss-client.php';

/**
* Class Syndication_Client_Factory
*/
class Syndication_Client_Factory {

public static function get_client( $transport_type, $site_ID ) {

/**
* @param $transport_type
* @param $site_id
*
* @return mixed
* @throws Exception
*/
public static function get_client( $transport_type, $site_id ) {
$class = self::get_transport_type_class( $transport_type );
if( class_exists( $class ) ) {
return new $class( $site_ID );
if ( class_exists( $class ) ) {
return new $class( $site_id );
}

throw new Exception(' transport class not found' );

throw new Exception( ' transport class not found' );
}

/**
* @param $site
* @param $transport_type
*
* @return false|mixed
* @throws Exception
*/
public static function display_client_settings( $site, $transport_type ) {

$class = self::get_transport_type_class( $transport_type );
if( class_exists( $class ) ) {
if ( class_exists( $class ) ) {
return call_user_func( array( $class, 'display_settings' ), $site );
}

throw new Exception( 'transport class not found: '. $class );

throw new Exception( 'transport class not found: ' . $class );
}

public static function save_client_settings( $site_ID, $transport_type ) {

/**
* @param $site_id
* @param $transport_type
*
* @return false|mixed
* @throws Exception
*/
public static function save_client_settings( $site_id, $transport_type ) {
$class = self::get_transport_type_class( $transport_type );
if( class_exists( $class ) ) {
return call_user_func( array( $class, 'save_settings' ), $site_ID );
if ( class_exists( $class ) ) {
return call_user_func( array( $class, 'save_settings' ), $site_id );
}

throw new Exception( 'transport class not found' );

}

/**
* @param $transport_type
*
* @return string
*/
public static function get_transport_type_class( $transport_type ) {
return 'Syndication_' . $transport_type . '_Client';
}
Expand Down
21 changes: 11 additions & 10 deletions includes/class-syndication-event-counter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php


/**
* Event Counter
*
* This allows for generic events to be captured and counted. Use the push_syndication_event and push_syndication_reset_event actions to capture and reset counters. Use push_syndication_after_event to handle events once they've occurred, and to see the number of times the event has occurred.
*/

class Syndication_Event_Counter {

/**
Expand All @@ -19,18 +20,18 @@ public function __construct() {
/**
* Increments an event counter.
*
* @param string $event_slug An identifier for the event.
* @param string $event_slug An identifier for the event.
* @param string|int $event_object_id An identifier for the object the event is associated with. Should be unique across all objects associated with the given $event_slug.
*/
public function count_event( $event_slug, $event_object_id = null ) {
// Coerce the slug and ID to strings. PHP will fire appropriate warnings if the given slug and ID are not coercible.
$event_slug = (string) $event_slug;
$event_slug = (string) $event_slug;
$event_object_id = (string) $event_object_id;

// Increment the event counter.
$option_name = $this->_get_safe_option_name( $event_slug, $event_object_id );
$count = get_option( $option_name, 0 );
$count = $count + 1;
$option_name = $this->get_safe_option_name( $event_slug, $event_object_id );
$count = get_option( $option_name, 0 );
$count = ++$count;
update_option( $option_name, $count );

/**
Expand Down Expand Up @@ -61,10 +62,10 @@ public function count_event( $event_slug, $event_object_id = null ) {
*/
public function reset_event( $event_slug, $event_object_id ) {
// Coerce the slug and ID to strings. PHP will fire appropriate warnings if the given slug and ID are not coercible.
$event_slug = (string) $event_slug;
$event_slug = (string) $event_slug;
$event_object_id = (string) $event_object_id;

delete_option( $this->_get_safe_option_name( $event_slug, $event_object_id ) );
delete_option( $this->get_safe_option_name( $event_slug, $event_object_id ) );
}

/**
Expand All @@ -76,7 +77,7 @@ public function reset_event( $event_slug, $event_object_id ) {
* @param $event_object_id
* @return string
*/
protected function _get_safe_option_name( $event_slug, $event_object_id ) {
protected function get_safe_option_name( $event_slug, $event_object_id ) {
return 'push_syndication_event_counter_' . md5( $event_slug . $event_object_id );
}
}
}
Loading