pf_replace_author_presentation() warns "Attempt to read property ID on null" when there is no global $post

Aperta Adatta ai principianti
#1,296 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php, wordpress
Ambito
backend

Direzione di ricerca

Apri includes/functions.php e leggi pf_replace_author_presentation() insieme a pf_replace_author_uri_presentation(), citata nell’issue come controllo simile. Se presenti, controlla i test esistenti per questi filtri dell’autore. Il lavoro è terminato quando le chiamate senza un WP_Post globale restituiscono l’autore originale senza attivare l’avviso PHP.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

pf_replace_author_presentation() in includes/functions.php is hooked to the_author and get_the_author_display_name, and reads $post->ID from the global $post without checking it exists:

function pf_replace_author_presentation( $author ) {
	global $post;

	if ( 'yes' !== get_option( 'pf_present_author_as_primary', 'yes' ) ) {
		return $author;
	}

	// If this doesn't look like a PF item, return the author.
	$item_link = get_post_meta( $post->ID, 'item_link', true );

Those filters also run where there is no global post: admin-ajax requests, network admin screens, and anything else that calls get_the_author_meta( 'display_name' ) or the_author() outside the loop. On PHP 8 every such call logs:

PHP Warning: Attempt to read property "ID" on null in .../pressforward/includes/functions.php on line 955

We saw 137 of these in about three minutes on a multisite network (5.9.6, PHP 8.4), from admin-ajax on two sites and wp-admin/network/update.php. The same code is on the default branch.

Suggested fix

Return early when there is no post, as pf_replace_author_uri_presentation() just below already does:

function pf_replace_author_presentation( $author ) {
	global $post;

	if ( ! $post instanceof WP_Post ) {
		return $author;
	}
	…

(Related but separate: #1226.)

Lingua principale
PHP
Stelle
127
Fork
23
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PressForward/pressforward

Tutte le issue di PressForward/pressforward

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.