*/ private function rows() { $settings = PingPush_Settings::get(); $rows = isset( $settings['custom_hooks'] ) && is_array( $settings['custom_hooks'] ) ? $settings['custom_hooks'] : array(); return array_slice( array_values( array_filter( $rows, static function ( $row ) { return is_array( $row ) && ! empty( $row['hook'] ) && true === self::validate( $row['hook'] ); } ) ), 0, self::MAX_HOOKS ); } /** * Register one event per row. */ public function register_events() { foreach ( $this->rows() as $row ) { PingPush_Events::register( self::event_id( $row['hook'] ), array( /* translators: %s: hook name. */ 'label' => sprintf( __( 'Custom hook: %s', 'pingpush' ), $row['hook'] ), 'group' => 'other', 'source' => 'custom', 'title' => '' !== (string) ( $row['title'] ?? '' ) ? $row['title'] : '{hook}', 'message' => '' !== (string) ( $row['message'] ?? '' ) ? $row['message'] : __( 'The {hook} action ran.', 'pingpush' ), 'priority' => (int) ( $row['priority'] ?? 0 ), 'sound' => (string) ( $row['sound'] ?? '' ), 'enabled' => ! empty( $row['enabled'] ), 'placeholders' => array( 'hook', 'arg1', 'arg2', 'arg3', 'arg4', 'arg5' ), ) ); } } /** * Attach a listener to each hook. */ public function hooks() { foreach ( $this->rows() as $row ) { if ( empty( $row['enabled'] ) ) { continue; } $hook = (string) $row['hook']; $id = self::event_id( $hook ); // Works for actions and filters: always hands back the first argument unchanged. add_action( $hook, static function ( ...$args ) use ( $hook, $id ) { if ( ! PingPush_Queue::is_sending() ) { $data = array( 'hook' => $hook ); for ( $i = 0; $i < 5; $i++ ) { $data[ 'arg' . ( $i + 1 ) ] = array_key_exists( $i, $args ) ? self::describe( $args[ $i ] ) : ''; } PingPush_Events::trigger( $id, $data, array( 'url' => admin_url(), 'url_title' => __( 'Open dashboard', 'pingpush' ), 'dedupe' => 'request', 'dedupe_ttl' => 0, ) ); } return $args[0] ?? null; }, 999, 5 ); } } /** * Readable text for a hook argument. * * @param mixed $value Value. * @return string */ public static function describe( $value ) { if ( $value instanceof WP_Post ) { return get_the_title( $value ); } if ( $value instanceof WP_User ) { return $value->user_login; } if ( $value instanceof WP_Comment ) { return $value->comment_author; } if ( $value instanceof WP_Error ) { return $value->get_error_message(); } if ( is_object( $value ) ) { return get_class( $value ); } $text = PingPush_Templates::stringify( $value ); return PingPush_Templates::truncate( $text, 200 ); } }