• Resolved abitofmind

    (@abitofmind)


    1) Question to please answer here and in the help/docs:

    Regular Expression Features

    /wp-admin/options-general.php?page=mla-settings-menu-documentation&mla_tab=documentation#mla_regular_expressions

    The ,replace(p,r,v) option/format value

    To specify an array argument enclose the argument in braces ( “{” and “}” ) and separate elements with spaces. For a simple example, ,replace( {'/,/' '/-/'}, {' ' '_'} ) will change commas to spaces and dashes to underscores, while ,replace( {'/,/' '/-/'}, ' ' ) will change both commas and dashes to spaces.

    Question:

    If you replace an array with an equally long array like {'/from1/' '/from2/' '/from3/'}, {'to1' 'to2' 'to3'} do all transformations happen in one pass, or are the transformation pairs processed in a pipe?

    • That is the whole string gets processed through the first from→to pair,
    • then the second from→to pair,
    • and then eventually the last from→to pair?

    In my example: {'/\s+-\s+/' '/\s+/' '/?/'}, {'?' '-' '--'}

    1. Transform the special sequence space-hyphen-space to a bullet as my temporary unique placeholder.
    2. Then transform any successive whitespace characters uniformly to a single hyphen.
    3. Then transform my temporary unique placeholder, the bullet, into a double hyphen.

    Example input: Main topic - Sub topic - Remark at the end.jpg

    Should become: Main-topic--Sub-topic--Remark-at-the-end.jpg

    If this is not processed in a pipe, but in a single pass, then I would simply need to run 3 consecutive Bulk Edit UI operations, each containing only a single from→to pair. Which is also ok if needed only rarely.

    2) Usability feedback: I think the syntax of two separate arrays whose pairs are determined by their positional counterpart is possibly closest to the technical model you use, but not at all the mental model of the user intent. Easier to read and mentally process would have been a single array with consecutive from/to pairs, like replace{ ‘/from1/’ , ‘to1’ , ‘/from2/’ , ‘to2’ , ‘/from3/’ , ‘to3’ }. Just a feedback.

    3) Bug report: RegEx replace() function with arrays fails with Uncaught TypeError for trim()

    • WordPress → Media → Assistant → Selecting 3 images > Bulk actions → Apply → Bulk Edit UI opens
    • Filling “ALT Text” with the help example:
    template:[+post_title,replace( {'/,/' '/-/'}, {' ' '_'} )+]
    • Results in this error message in the Admin UI:
    An ajax.fail error has occurred. Please reload the page and try again. (error), jqXHR( 500, error, <p>There has been a critical error on this website.</p><p><a href="https://www.ads-software.com/documentation/article/faq-troubleshooting/">Learn more about troubleshooting WordPress.</a></p>)
    • And this in php_error.log :
    [23-May-2023 08:06:46 UTC] PHP Fatal error:  Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, array given in /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php:1260
    Stack trace:
    #0 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php(1260): trim()
    #1 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data-source.php(500): MLAData::mla_apply_field_level_format()
    #2 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data-source.php(179): MLAData_Source::_evaluate_data_source()
    #3 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-options.php(860): MLAData_Source::mla_get_data_source()
    #4 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(984): MLAOptions::mla_get_data_source()
    #5 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1046): MLA::_process_bulk_value()
    #6 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1386): MLA::mla_prepare_bulk_edits()
    #7 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1877): MLA::mla_process_bulk_action()
    #8 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1898): MLA::_bulk_edit_ajax_handler()
    #9 /data/web/customer-id/html/apps/domain-directory/wp-includes/class-wp-hook.php(308): MLA::mla_inline_edit_ajax_action()
    #10 /data/web/customer-id/html/apps/domain-directory/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()
    #11 /data/web/customer-id/html/apps/domain-directory/wp-includes/plugin.php(517): WP_Hook->do_action()
    #12 /data/web/customer-id/html/apps/domain-directory/wp-admin/admin-ajax.php(188): do_action()
    #13 {main}
      thrown in /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php on line 1260
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your detailed description and example. Thanks as well for the bug report.

    MLA’s Regular Expression features use the PHP “PCRE Functions”, and the MLA syntax is as close to the PHP syntax as possible. Regarding your first question, I do not know how preg_replace() handles array arguments and the PHP documentation isn’t clear. I suggest you try an experiment and share your results here so I can update the MLA Documentation.

    Regarding your usability feedback, my policy with all of the PHP-based format codes has been to match the PHP syntax as closely as possible, because many MLA users are familiar with the PHP equivalents.

    Regarding your bug report, I was able to reproduce and fix this MLA defect and I thank you for reporting it.

    I have uploaded a new MLA Development Version dated 20230524 that corrects the problem. You can find step-by-step instructions for using the Development Version in this earlier topic:

    PHP Warning on media upload with Polylang

    Once the Development Version is installed you can retest the handling of array arguments in the RegEx replace() function.

    The fix will be part of my next MLA version, but in the interim it would be great if you could install the Development Version and let me know if it works for you. Thanks for alerting me to this MLA defect.

    Thread Starter abitofmind

    (@abitofmind)

    Ad 1) Question: preg_replace() find/replace pairs processed together or sequentially (in pipe) ?

    Thanks for telling that you don’t know. Will report my experiences at a later time if I get to it.

    Ad 2) Usability feedback: You stick to the PHP RegEx syntax {'/from1/' '/from2/' '/from3/'}, {'to1' 'to2' 'to3'} rather than the more readable {'/from1/', 'to1', '/from2/', 'to2', '/from3/', 'to3'} in order to be easy to handle for those familiar with it.

    Was just an idea. No problem.

    Ad 3) Bug fix

    Read how to update to development version. Seems simple. So wanted to test it. But you did not mention where from to download that development-build without doubt (you say it has the same version number as the current stable build, hence I did not want to resort to guesswork). Did not find it myself. Please provide me the link to the build version. Thanks!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update. I look forward to your experimenting with the together/pipeline behavior.

    The Development Version link is in the earlier topic, where it says:

    To get the Development Version, click this link to download the Development Version ZIP archive:

    https://downloads.www.ads-software.com/plugin/media-library-assistant.zip

    Let me know if you have any trouble with downloading.

    Thread Starter abitofmind

    (@abitofmind)

    I beg your pardon. I have read the instructions. But saw the post you referred to is over 5 years old. And hence though the link there is not a current one. Only now with your extra hint I understood this is a generic URL always pointing to the most recent dev-build.

    Now inspected the ZIP: readme.txt in root says “Stable tag: 3.07” but the changelog has already a “3.08” section, with one line “Fix: For the ,replace(p,r,v) option/format value, an array of patterns (p) is now handled properly.” So now I’m sure I got the right version. ??

    Will report when tested.

    Thread Starter abitofmind

    (@abitofmind)

    Ad 3) Bug fix: Tested in dev-version-20230524.

    • ? Help example for replace() with arrays worked.

    Ad 1) Even better news: replace() function with arrays work in sequence: First your input gets through the first src→dst pair, then through src2→dst2, until finally … srcX→dstX.

    • ? Works in Bulk Edit UI.
    • ? Works also when having Enable IPTC/EXIF mappings when adding new media ON. Which is great!

    My application:

    WordPress → MLA → Settings → EXIF/IPTC tab → Alt text field → Uses this active mapping:

    template:[+post_title,replace( {'/( +(?!\\d{4,})(\\d{1}[A-Za-z]{0,1})+ *)|( +([a-uw-z]\\d+)([a-z]\\d*)* *)/' '/ +-+ +$/' '/- #(?=[a-z])/' '/#(?=\\d+)/'}, {' - ' '' '' ''} )+]

    Which is equivalent if split into 4 separate find/replace pairs, executed one after the other in the Bulk Edit UI, explained with an inline comments:

    # 1) Replace ordinal number/letter portion like 1 or 1a or 1a1 or 1a1b or B1 or B1c with the uniform separator " - "
    template:[+post_title,replace( {'/( +(?!\\d{4,})(\\d{1}[A-Za-z]{0,1})+ *)|( +([a-uw-z]\\d+)([a-z]\\d*)* *)/'}, {' - '} )+]
    
    # 2) Remove that separator if at the line end
    #    - So multiple images will have the same ALT text
    #    - Add differentiation manually where needed)
    template:[+alt_text,replace( {'/ +-+ +$/'}, {''} )+]
    
    # 3) Remove pound symbol which protected intentional lowercase letters after ordinal numbers in earlier transformations
    template:[+alt_text,replace( {'/- #(?=[a-z])/'}, {''} )+]
    
    # 4) Remove pound symbol protecting literal numbers
    template:[+alt_text,replace( {'/#(?=\\d+)/'}, {''} )+]

    Gotchas:

    • RegEx character classes with a backslash like \d for “any digit” need two backslashes to “make it through”, so \\d.
      ?? Please add that to the RegEx documentation section. So far it only mentions that square and curly brackets need two prefixed backslashes for escaping.
    • The RegEx pattern (even if just a literal character like “x”) needs to wrapped in single quotes and slashes, so for a literal x this needs to be '/x/'.
      ?? Please also mention this in the docs.
    • The substitution needs to be in single quotes. No slashes around needed.

    My filename transformation principle:

    Original file name on my computer:

    Main Topic 12a2 Sub Topic - Remark potentially with a literal #1 and over.webp
    • “Main Topic” is the only minimal compulsory name segment.
    • “Sub Topic” is optional.
    • “Remark at the end” is optional.
    • The ordinal number/letter combo (e.g. 12a) may be at any position, where it makes sense or is required (unique filenames).

    Sanitized filenames from WordPress:

    Main-Topic-12a2-Sub-Topic-Remark-potentially-with-a-literal-1-and-over.webp

    Media title:

    Main Topic 12a2 Sub Topic - Remark potentially with a literal #1 and over

    Alt text after it went through the 4 replacements:

    Main Topic - Sub Topic - Remark potentially with a literal 1 and over
    • The ordinal number only helps file sorting and media library sorting (by media title), for images which require a certain presentation order. For the ALT title this portion carries no information values and hence it is replaced by a ” – ” separator.
    • The literal “1” was protected with a pound in front of it so that it does not falsely get matched during ordinal number replacement. I also protected the letter “v” in front of a number for denoting a version number to be not interpreted as a purely ordinal number/letter portion, thus the RegEx [a-uw-z].
    • This reply was modified 1 year, 6 months ago by abitofmind.
    Plugin Author David Lingren

    (@dglingren)

    Thank you for the detailed write up of your application solution. This will be very useful for other MLA users and I appreciate the time you put in to it. Thanks as well for reporting the “Even better news:?replace() function with arrays work in sequence” results. I will add that to the Documentation tab.

    Regarding the double-backslash gotcha, the “Regular Expression Features” section of the Documentation already contains this statement in the “Patterns and subpatterns” subsection:

    The patterns above contain backslash (‘\’) characters. When entering backslashes in an argument of the three option/format values below?you must code two backslashes?because the backslash is used in function arguments to escape special characters such as quotes (” ‘ “). The examples below show how this is done.

    All of the “Application Examples” later in the section include the double backslashes. I am not sure what else to add.

    Regarding the “single quotes and slashes” gotcha, I have added a paragraph to the “Patterns and subpatterns” subsection to emphasize this using your example. Thanks for that suggestion.

    Thread Starter abitofmind

    (@abitofmind)

    Thanks for your appreciation!

    Thanks that you double checked your documentation and amended it where necessary/beneficiary. In particular also glad that the double backslash hint was already there in the appropriate section, I must have missed it.

    I think for now we improved it to the maximum!

    Thanks for the great collaboration!

    • This reply was modified 1 year, 5 months ago by abitofmind.
    • This reply was modified 1 year, 5 months ago by abitofmind.
    Plugin Author David Lingren

    (@dglingren)

    I have released MLA v3.09, which contains the fix described in my previous post. I am marking this topic resolved, but please update it if you have any problems with this latest update. Thanks for working with me on this topic.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘RegEx ,replace() function with arrays: Fails with Uncaught TypeError’ is closed to new replies.