Edent,
@Edent@mastodon.social avatar

nerds - I've a tricky problem that I can't find the answer to.

I want to patch a single PHP file inside a 3rd party plugin.
I can overwrite the file, and my new code works.
But when the plugin is updated, the original file replaces my patched version.

I've tried changing the permissions of the file to read-only, but that doesn't work.

Is there any sensible way to make my changes permanent?

igrok,

@Edent if said plugin is packaged by RPM, you could create an empty package that requires plugin-with-fix and then repackage the plugin yourself with your patch and have it also provide plugin-with-fix. Then install both. When you upgrade, the upgrade will fail (notifying you of the update) until you repackage the update. If the original package sources are in git, you could use it to merge your modified package sources with their updates.

Or you could just edit the file and chattr +i

Edent,
@Edent@mastodon.social avatar

@igrok are WordPress files usually packaged with RPM?

igrok,

@Edent if you get them from a Linux distro (I assume Debian packages have similar capabilities)

If not, think of it as brainstorming

Edent,
@Edent@mastodon.social avatar

@igrok WordPress plugins are distributed ZIP files with PHP in them.

Perhaps refrain from trying to brainstorm until you have the basics covered?

mattround,
@mattround@crispsandwi.ch avatar

@Edent Could perhaps use this hook to re-overwrite it after each update..? https://developer.wordpress.org/reference/hooks/upgrader_process_complete/

Edent,
@Edent@mastodon.social avatar

@mattround
Aha! That might do it. Will investigate. Cheers.

kraftner,
@kraftner@mastodon.social avatar

@Edent Just to have this out of the way: Patching code like this most of the time isn't a good idea and will cause you pain in the future.
Now: What exactly are you trying to change? In WP usually the way to go is trying to do whatever you are trying to do using actions/filters. If the code is open source I can have a look if that is possible if you point me at the code in question.

Edent,
@Edent@mastodon.social avatar

@kraftner yeah, I know.
This is what I'm trying to do
https://github.com/michelf/php-markdown/issues/387

My patch works (for me) - but the file is deep within JetPack. I'm not entirely sure which filter I would set up to intercept it.

kraftner,
@kraftner@mastodon.social avatar

@Edent First: Using the title attribute for tooltips isn't a great idea: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/title#accessibility_concerns
But back to your actual question. Jetpack by now is a convoluted legacy codebase. So while I'm quite sure it is doable I don't have the time right now to look into it. At first glance it looks as if the parser actually used is instantiated here: https://github.com/Automattic/jetpack/blob/a8c42254e8f804ff2cb1afcee24e1d0f68a52679/projects/plugins/jetpack/modules/markdown/easy-markdown.php#L389-L397
Since it hardcodes the path and saves it in a private property it is hard to alter without replacing everything leading up to there.

kraftner,
@kraftner@mastodon.social avatar

@Edent So what I'd actually do is something completely different: Hook on https://developer.wordpress.org/reference/hooks/the_content/ and use https://developer.wordpress.org/news/2023/09/28/the-html-api-process-your-tags-not-your-pain/ to look for links that have the jetpack-footnote CSS class. Then look for the corresponding li in the footer with that ID, extract the text and replace the title tag in the content link.
This should be forward compatible, isn't much code and will just stop working instead of taking your whole site down if Jetpack ever does e.g. a major refactoring.

Edent,
@Edent@mastodon.social avatar

@kraftner
I'm comfortable with the tootips as the link to the text is still present.
Thanks for the other tips. I'll check them out.

Edent,
@Edent@mastodon.social avatar

@kraftner Hmmm. Looks like it might be trickier than I thought.

> it’s not possible for the Tag Processor to return the inner markup inside an element.
https://developer.wordpress.org/reference/classes/wp_html_tag_processor/#design-and-limitations

So I can find the footnote links - and associated li - but I can't get the text of that li.

Will keep fiddling around. Cheers!

kraftner,
@kraftner@mastodon.social avatar

@Edent Oh, yeah, right, forgot about that, sorry. Then you'll need https://www.php.net/manual/de/class.domdocument.php or some other library like https://github.com/wasinger/htmlpagedom but I'd still code it in a way that does as much as possible with the HTML API from WordPress so you can swap the rest (getting the text content) if it at some later point gains that feature.

Edent,
@Edent@mastodon.social avatar

@kraftner good tip. Thanks.

darkling,
@darkling@mstdn.social avatar

@Edent It's not particularly sensible, but the big hammer is setting the immutable attribute: chattr +i <file>

Use this only if you can't find another way.

Edent,
@Edent@mastodon.social avatar

@darkling might be a last resort 😆

pwaring,
@pwaring@fosstodon.org avatar

@Edent Despite the name, I think you can accomplish this using a child theme, which inherits all the plugins of its parent but can override them (you can't have a grandchild theme though, so this doesn't work if you're already using a child theme).

Edent,
@Edent@mastodon.social avatar

@pwaring interesting, I'll take a look.

dominik,
@dominik@nona.social avatar

@Edent if there is, I didn't find it, yet. I've tried with automatic patching before, that went about as well as expected. So far the best way I found was to upstream the patch and hope that the maintainer accepts it.

Edent,
@Edent@mastodon.social avatar

@dominik So far, no dice!
https://github.com/michelf/php-markdown/issues/387
(Possibly because my code isn't brilliant and it changes people's expectations.)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • wordpress
  • ethstaker
  • DreamBathrooms
  • cubers
  • magazineikmin
  • InstantRegret
  • GTA5RPClips
  • thenastyranch
  • Youngstown
  • rosin
  • slotface
  • osvaldo12
  • ngwrru68w68
  • kavyap
  • everett
  • JUstTest
  • Durango
  • Leos
  • cisconetworking
  • mdbf
  • khanakhh
  • normalnudes
  • tester
  • modclub
  • anitta
  • tacticalgear
  • megavids
  • provamag3
  • lostlight
  • All magazines