Skip to content

Commit

Permalink
PHPCS: Include all files by default (#930)
Browse files Browse the repository at this point in the history
* PHPCS: Include all files by default

* simplify menu highlighting

* Update includes/transformer/class-factory.php

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
  • Loading branch information
obenland and pfefferle authored Oct 9, 2024
1 parent f8cd515 commit ef66677
Show file tree
Hide file tree
Showing 25 changed files with 250 additions and 164 deletions.
12 changes: 6 additions & 6 deletions includes/class-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class Notification {
/**
* Notification constructor.
*
* @param string $type The type of the notification.
* @param string $actor The actor URL.
* @param array $object The Activity object.
* @param int $target The WordPress User-Id.
* @param string $type The type of the notification.
* @param string $actor The actor URL.
* @param array $activity The Activity object.
* @param int $target The WordPress User-Id.
*/
public function __construct( $type, $actor, $object, $target ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
public function __construct( $type, $actor, $activity, $target ) {
$this->type = $type;
$this->actor = $actor;
$this->object = $object;
$this->object = $activity;
$this->target = $target;
}

Expand Down
6 changes: 3 additions & 3 deletions includes/class-signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static function generate_signature( $user_id, $http_method, $url, $date,

$signature = null;
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
$signature = \base64_encode( $signature ); // phpcs:ignore
$signature = \base64_encode( $signature ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

$key_id = $user->get_url() . '#main-key';

Expand Down Expand Up @@ -345,7 +345,7 @@ public static function get_remote_key( $key_id ) {
);
}
if ( isset( $actor['publicKey']['publicKeyPem'] ) ) {
return \rtrim( $actor['publicKey']['publicKeyPem'] ); // phpcs:ignore
return \rtrim( $actor['publicKey']['publicKeyPem'] );
}
return new WP_Error(
'activitypub_no_remote_key_found',
Expand Down Expand Up @@ -400,7 +400,7 @@ public static function parse_signature_header( $signature ) {
$parsed_header['headers'] = \explode( ' ', trim( $matches[1] ) );
}
if ( \preg_match( '/signature="(.*?)"/ism', $signature, $matches ) ) {
$parsed_header['signature'] = \base64_decode( preg_replace( '/\s+/', '', trim( $matches[1] ) ) ); // phpcs:ignore
$parsed_header['signature'] = \base64_decode( preg_replace( '/\s+/', '', trim( $matches[1] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
}

if ( ( $parsed_header['signature'] ) && ( $parsed_header['algorithm'] ) && ( ! $parsed_header['headers'] ) ) {
Expand Down
6 changes: 3 additions & 3 deletions includes/collection/class-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function remove_follower( $user_id, $actor ) {
public static function get_follower( $user_id, $actor ) {
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT DISTINCT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = 'activitypub_user_id' AND pm.meta_value = %d AND p.guid = %s",
Expand All @@ -119,12 +119,12 @@ public static function get_follower( $user_id, $actor ) {
*
* @param string $actor The Actor URL.
*
* @return Follower|null The Follower object or null.
* @return \Activitypub\Activity\Base_Object|WP_Error|null
*/
public static function get_follower_by_actor( $actor ) {
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
Expand Down
3 changes: 1 addition & 2 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,8 +1012,7 @@ function ( $enclosure ) {
function get_comment_ancestors( $comment ) {
$comment = \get_comment( $comment );

// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( ! $comment || empty( $comment->comment_parent ) || $comment->comment_parent == $comment->comment_ID ) {
if ( ! $comment || empty( $comment->comment_parent ) || (int) $comment->comment_parent === (int) $comment->comment_ID ) {
return array();
}

Expand Down
58 changes: 38 additions & 20 deletions includes/handler/class-announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,25 @@ public static function init() {
/**
* Handles "Announce" requests.
*
* @param array $array The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \Activitypub\Activity\Activity $activity The activity object.
*
* @return void
* @param array $announcement The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \Activitypub\Activity\Activity $activity The activity object.
*/
public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
public static function handle_announce( $announcement, $user_id, $activity = null ) {
// Check if Activity is public or not.
if ( ! is_activity_public( $array ) ) {
if ( ! is_activity_public( $announcement ) ) {
// @todo maybe send email
return;
}

if ( ! ACTIVITYPUB_DISABLE_REACTIONS ) {
self::maybe_save_announce( $array, $user_id, $activity );
self::maybe_save_announce( $announcement, $user_id );
}

if ( is_string( $array['object'] ) ) {
$object = Http::get_remote_object( $array['object'] );
if ( is_string( $announcement['object'] ) ) {
$object = Http::get_remote_object( $announcement['object'] );
} else {
$object = $array['object'];
$object = $announcement['object'];
}

if ( ! $object || is_wp_error( $object ) ) {
Expand All @@ -66,21 +64,33 @@ public static function handle_announce( $array, $user_id, $activity = null ) { /

$type = \strtolower( $object['type'] );

/**
* Fires after an Announce has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
*/
\do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );

/**
* Fires after an Announce of a specific type has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
*/
\do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
}

/**
* Try to save the Announce.
*
* @param array $array The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \Activitypub\Activity\Activity $activity The activity object.
*
* @return void
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
*/
public static function maybe_save_announce( $array, $user_id, $activity ) { // phpcs:ignore
$url = object_to_uri( $array['object'] );
public static function maybe_save_announce( $activity, $user_id ) {
$url = object_to_uri( $activity['object'] );

if ( empty( $url ) ) {
return;
Expand All @@ -91,13 +101,21 @@ public static function maybe_save_announce( $array, $user_id, $activity ) { // p
return;
}

$state = Interactions::add_reaction( $array );
$state = Interactions::add_reaction( $activity );
$reaction = null;

if ( $state && ! is_wp_error( $state ) ) {
$reaction = get_comment( $state );
}

do_action( 'activitypub_handled_announce', $array, $user_id, $state, $reaction );
/**
* Fires after an Announce has been saved.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param mixed $state The state of the reaction.
* @param mixed $reaction The reaction.
*/
do_action( 'activitypub_handled_announce', $activity, $user_id, $state, $reaction );
}
}
23 changes: 14 additions & 9 deletions includes/handler/class-like.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ public static function init() {
/**
* Handles "Like" requests.
*
* @param array $array The Activity array.
* @param int $user_id The ID of the local blog user.
* @param \Activitypub\Activity\Activity $activity The Activity object.
*
* @return void
* @param array $like The Activity array.
* @param int $user_id The ID of the local blog user.
*/
public static function handle_like( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound,VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable,Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
public static function handle_like( $like, $user_id ) {
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return;
}

$url = object_to_uri( $array['object'] );
$url = object_to_uri( $like['object'] );

if ( empty( $url ) ) {
return;
Expand All @@ -53,13 +50,21 @@ public static function handle_like( $array, $user_id, $activity = null ) { // ph
return;
}

$state = Interactions::add_reaction( $array );
$state = Interactions::add_reaction( $like );
$reaction = null;

if ( $state && ! is_wp_error( $state ) ) {
$reaction = get_comment( $state );
}

do_action( 'activitypub_handled_like', $array, $user_id, $state, $reaction );
/**
* Fires after a Like has been handled.
*
* @param array $like The Activity array.
* @param int $user_id The ID of the local blog user.
* @param mixed $state The state of the reaction.
* @param mixed $reaction The reaction object.
*/
do_action( 'activitypub_handled_like', $like, $user_id, $state, $reaction );
}
}
2 changes: 1 addition & 1 deletion includes/model/class-follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function save() {
if ( ! $this->get__id() ) {
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s",
Expand Down
2 changes: 1 addition & 1 deletion includes/rest/class-outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function user_outbox_get( $request ) {
$json->next = \add_query_arg( 'page', $page + 1, $json->partOf );
}

if ( $page && ( $page > 1 ) ) { // phpcs:ignore
if ( $page && ( $page > 1 ) ) {
$json->prev = \add_query_arg( 'page', $page - 1, $json->partOf );
}
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
Expand Down
10 changes: 3 additions & 7 deletions includes/table/class-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,22 @@ public function prepare_items() {

$args = array();

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['orderby'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['orderby'] = sanitize_text_field( wp_unslash( $_GET['orderby'] ) );
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['order'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['order'] = sanitize_text_field( wp_unslash( $_GET['order'] ) );
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['s'] ) && isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
if ( wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$args['s'] = sanitize_text_field( wp_unslash( $_GET['s'] ) );
}
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended

$followers_with_count = FollowerCollection::get_followers_with_count( $this->user_id, $per_page, $page_num, $args );
$followers = $followers_with_count['followers'];
Expand Down Expand Up @@ -219,7 +215,7 @@ public function process_action() {
return;
}

$followers = $_REQUEST['followers']; // phpcs:ignore
$followers = $_REQUEST['followers']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput

if ( $this->current_action() === 'delete' ) {
if ( ! is_array( $followers ) ) {
Expand Down
23 changes: 12 additions & 11 deletions includes/transformer/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
use WP_Error;

/**
* Transformer Factory
* Transformer Factory.
*/
class Factory {
/**
* Get the transformer for a given object.
*
* @param mixed $object The object to transform.
* @param mixed $data The object to transform.
*
* @return Base|WP_Error The transformer to use, or an error.
*/
public static function get_transformer( $object ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
if ( ! \is_object( $object ) ) {
public static function get_transformer( $data ) {
if ( ! \is_object( $data ) ) {
return new WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
}

$class = \get_class( $object );
$class = \get_class( $data );

/**
* Filter the transformer for a given object.
Expand All @@ -50,12 +51,12 @@ public static function get_transformer( $object ) { // phpcs:ignore Universal.Na
* }, 10, 3 );
*
* @param Base $transformer The transformer to use.
* @param mixed $object The object to transform.
* @param mixed $data The object to transform.
* @param string $object_class The class of the object to transform.
*
* @return mixed The transformer to use.
*/
$transformer = \apply_filters( 'activitypub_transformer', null, $object, $class );
$transformer = \apply_filters( 'activitypub_transformer', null, $data, $class );

if ( $transformer ) {
if (
Expand All @@ -71,12 +72,12 @@ public static function get_transformer( $object ) { // phpcs:ignore Universal.Na
// Use default transformer.
switch ( $class ) {
case 'WP_Post':
if ( 'attachment' === $object->post_type ) {
return new Attachment( $object );
if ( 'attachment' === $data->post_type ) {
return new Attachment( $data );
}
return new Post( $object );
return new Post( $data );
case 'WP_Comment':
return new Comment( $object );
return new Comment( $data );
default:
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,8 @@ protected function get_post_content_template() {
$template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
break;
default:
// phpcs:ignore Universal.Operators.DisallowShortTernary.Found
$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ) ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
$content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
$template = empty( $content ) ? ACTIVITYPUB_CUSTOM_POST_CONTENT : $content;
break;
}

Expand Down
7 changes: 3 additions & 4 deletions integration/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ function plugin_init() {
if ( \defined( 'SSP_VERSION' ) ) {
add_filter(
'activitypub_transformer',
// phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
function ( $transformer, $object, $object_class ) {
function ( $transformer, $data, $object_class ) {
if (
'WP_Post' === $object_class &&
\get_post_meta( $object->ID, 'audio_file', true )
\get_post_meta( $data->ID, 'audio_file', true )
) {
require_once __DIR__ . '/class-seriously-simple-podcasting.php';
return new Seriously_Simple_Podcasting( $object );
return new Seriously_Simple_Podcasting( $data );
}
return $transformer;
},
Expand Down
6 changes: 2 additions & 4 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?xml version="1.0"?>
<ruleset name="WordPress ActivityPub">
<description>WordPress ActivityPub Standards</description>
<file>./activitypub.php</file>
<file>./includes/</file>
<file>./integration/</file>
<file>./build/</file>
<file>.</file>

<exclude-pattern>*\.(inc|css|js|svg)</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*.asset.php</exclude-pattern>

<arg value="ps"/>
Expand Down
Loading

0 comments on commit ef66677

Please sign in to comment.