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

Fix more PHP 8.1 compatibility issues #1494

Merged
merged 19 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion alerts/class-alert-type-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function alert( $record_id, $recordarr, $alert ) {
$user = get_user_by( 'id', $user_id );

// translators: Placeholder refers to a username (e.g. "administrator").
$message .= sprintf( __( "User:\t%s", 'stream' ), $user->user_login ) . "\n";
$message .= sprintf( __( "User:\t%s", 'stream' ), ! empty( $user->user_login ) ? $user->user_login : __( 'unknown', 'stream' ) ) . "\n";

if ( ! empty( $alert->alert_meta['trigger_context'] ) ) {
$context = $this->plugin->alerts->alert_triggers['context']->get_display_value( 'list_table', $alert );
Expand Down
30 changes: 17 additions & 13 deletions alerts/class-alert-type-ifttt.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,24 @@ public function notify_ifttt( $alert, $recordarr ) {
)
);

$user_id = $recordarr['user_id'];
$user = get_user_by( 'id', $user_id );
$user_id = ! empty( $recordarr['user_id'] ) ? $recordarr['user_id'] : 0;
$user = get_user_by( 'id', $user_id );
$user_value = '';

/**
* Filter User data field
*
* Defaults to 'user_login'.
*
* @param object $alert The Alert.
* @param array $recordarray The Record's data.
* @return string
*/
$user_field = apply_filters( 'wp_stream_alert_ifttt_user_data_value', 'user_login', $alert, $recordarr );
$user_value = ! empty( $user->$user_field ) ? $user->$user_field : $user->user_login;
if ( $user instanceof \WP_User ) {
/**
* Filter User data field.
*
* Defaults to 'user_login'.
*
* @param object $alert The Alert object.
* @param array $recordarr Array of Record data.
*
* @return string
*/
$user_field = apply_filters( 'wp_stream_alert_ifttt_user_data_value', 'user_login', $alert, $recordarr );
$user_value = ! empty( $user->$user_field ) ? $user->$user_field : $user->user_login;
}

$created = $recordarr['created'];
/**
Expand Down
5 changes: 3 additions & 2 deletions classes/class-alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class Alert {
/**
* Class constructor
*
* @param object $item Alert data.
* @param Plugin $plugin Instance of plugin object.
* @param ?object $item Alert data.
* @param Plugin $plugin Instance of plugin object.
*
* @return void
*/
public function __construct( $item, $plugin ) {
Expand Down
11 changes: 6 additions & 5 deletions classes/class-alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,18 +410,19 @@ public function register_post_type() {
/**
* Return alert object of the given ID
*
* @param string $post_id Post ID for the alert.
* @param string|int $post_id Post ID for the alert.
*
* @return Alert
*/
public function get_alert( $post_id = '' ) {
if ( ! $post_id ) {
$obj = new Alert( null, $this->plugin );

return $obj;
return new Alert( null, $this->plugin );
}

$post = get_post( $post_id );
if ( ! ( $post instanceof \WP_Post ) ) {
return new Alert( null, $this->plugin );
}

$alert_type = get_post_meta( $post_id, 'alert_type', true );
$alert_meta = get_post_meta( $post_id, 'alert_meta', true );
Expand Down Expand Up @@ -565,7 +566,7 @@ public function load_alerts_settings() {
}
$alert = array();
$post_id = wp_stream_filter_input( INPUT_POST, 'post_id' );
if ( ! empty( $post_id ) ) {
if ( ! empty( $post_id ) && 'new' !== $post_id ) {
$alert = $this->get_alert( $post_id );
if ( false === $alert ) {
wp_send_json_error(
Expand Down
179 changes: 98 additions & 81 deletions classes/class-date-interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

namespace WP_Stream;

// Load Carbon to Handle dates much easier.
if ( ! class_exists( 'Carbon\Carbon' ) ) {
require_once wp_stream_get_instance()->locations['inc_dir'] . 'lib/Carbon.php';
}
use Carbon\Carbon;
bartoszgadomski marked this conversation as resolved.
Show resolved Hide resolved

/**
* Class - Date_Interval
*/
Expand Down Expand Up @@ -43,86 +37,109 @@ public function get_predefined_intervals() {

if ( empty( $timezone ) ) {
$gmt_offset = (int) get_option( 'gmt_offset' );
$timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true );
if ( false === $timezone ) {
$timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false );
}
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, true );
if ( false === $timezone ) {
$timezone = null;
$timezone = timezone_name_from_abbr( '', $gmt_offset * 3600, false );
}
}

return apply_filters(
'wp_stream_predefined_date_intervals',
array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => Carbon::today( $timezone )->startOfDay(),
'end' => Carbon::today( $timezone )->endOfDay(),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => Carbon::today( $timezone )->startOfDay()->subDay(),
'end' => Carbon::today( $timezone )->startOfDay()->subSecond(),
),
'last-7-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => Carbon::today( $timezone )->subDays( 7 ),
'end' => Carbon::today( $timezone ),
),
'last-14-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => Carbon::today( $timezone )->subDays( 14 ),
'end' => Carbon::today( $timezone ),
),
'last-30-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => Carbon::today( $timezone )->subDays( 30 ),
'end' => Carbon::today( $timezone ),
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => Carbon::today( $timezone )->startOfMonth(),
'end' => Carbon::today( $timezone )->endOfMonth(),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => Carbon::today( $timezone )->startOfMonth()->subMonth(),
'end' => Carbon::today( $timezone )->startOfMonth()->subSecond(),
),
'last-3-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => Carbon::today( $timezone )->subMonths( 3 ),
'end' => Carbon::today( $timezone ),
),
'last-6-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => Carbon::today( $timezone )->subMonths( 6 ),
'end' => Carbon::today( $timezone ),
),
'last-12-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => Carbon::today( $timezone )->subMonths( 12 ),
'end' => Carbon::today( $timezone ),
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => Carbon::today( $timezone )->startOfYear(),
'end' => Carbon::today( $timezone )->endOfYear(),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => Carbon::today( $timezone )->startOfYear()->subYear(),
'end' => Carbon::today( $timezone )->startOfYear()->subSecond(),
),
try {
$timezone_object = $timezone ? new \DateTimeZone( $timezone ) : null;
} catch ( \Exception $e ) {
$timezone_object = null;
}

try {
$today = new \DateTimeImmutable( 'today', $timezone_object );
$date_intervals = $this->generate_date_intervals( $today );
} catch ( \Exception $e ) {
$date_intervals = array();
}

/**
* Allow other plugins to filter the predefined date intervals.
*
* @param array $date_intervals Date intervals array.
* @param string $timezone Timezone.
*/
return apply_filters( 'wp_stream_predefined_date_intervals', $date_intervals, $timezone );
delawski marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Generate date intervals relative to date object provided.
*
* @param \DateTimeImmutable $date Date object.
*
* @return array[]
*/
public function generate_date_intervals( \DateTimeImmutable $date ) {
return array(
'today' => array(
'label' => esc_html__( 'Today', 'stream' ),
'start' => $date,
'end' => $date->modify( '+1 day -1 microsecond' ),
),
'yesterday' => array(
'label' => esc_html__( 'Yesterday', 'stream' ),
'start' => $date->modify( '-1 day' ),
'end' => $date->modify( '-1 microsecond' ),
),
'last-7-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ),
'start' => $date->modify( '-7 days' ),
'end' => $date,
),
'last-14-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ),
'start' => $date->modify( '-14 days' ),
'end' => $date,
),
'last-30-days' => array(
/* translators: %d: number of days (e.g. "7") */
'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ),
'start' => $date->modify( '-30 days' ),
'end' => $date,
),
'this-month' => array(
'label' => esc_html__( 'This Month', 'stream' ),
'start' => $date->modify( 'first day of this month' ),
'end' => $date->modify( 'last day of this month' )->modify( '+1 day -1 microsecond' ),
),
'last-month' => array(
'label' => esc_html__( 'Last Month', 'stream' ),
'start' => $date->modify( 'first day of last month' ),
'end' => $date->modify( 'last day of last month' )->modify( '+1 day -1 microsecond' ),
),
'last-3-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ),
'start' => $date->modify( '-3 months' ),
'end' => $date,
),
'last-6-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ),
'start' => $date->modify( '-6 months' ),
'end' => $date,
),
'last-12-months' => array(
/* translators: %d: number of months (e.g. "3") */
'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ),
'start' => $date->modify( '-12 months' ),
'end' => $date,
),
'this-year' => array(
'label' => esc_html__( 'This Year', 'stream' ),
'start' => $date->modify( 'first day of January' ),
'end' => $date->modify( 'last day of December' )->modify( '+1 day -1 microsecond' ),
),
'last-year' => array(
'label' => esc_html__( 'Last Year', 'stream' ),
'start' => $date->modify( 'first day of January' )->modify( '-1 year' ),
'end' => $date->modify( 'first day of January' )->modify( '-1 microsecond' ),
),
$timezone
);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"automattic/vipwpcs": "^3.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
"humanmade/mercator": "^1.0",
"johnbillion/query-monitor": "^3.16",
"php-coveralls/php-coveralls": "^2.5",
"phpcompatibility/php-compatibility": "dev-develop as 9.99.99",
"phpcompatibility/phpcompatibility-wp": "^2.1",
Expand Down
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion connectors/class-connector-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public function check_meta_values( $type, $action, $meta_id, $object_id, $key, $
/* translators: %1$s: a field label, %2$s: an object title, %3$s: an object type (e.g. "Message", "Hello World", "post") */
esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ),
array(
'field_label' => $cache['field']['label'],
'field_label' => ! empty( $cache['field']['label'] ) ? $cache['field']['label'] : __( 'unknown', 'stream' ),
'title' => $title,
'singular_name' => $type_name,
'meta_value' => $value,
Expand Down
Loading
Loading