Skip to content

Commit

Permalink
ACF Options Page Monitoring (#206)
Browse files Browse the repository at this point in the history
* Adds monitoring for acf option pages and introduces Utils class for helpers.

* formatting

* more formatting

* check if acf_get_options_pages is returning an array

Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>
  • Loading branch information
henrikwirth and TylerBarnes authored Feb 10, 2022
1 parent 45a9e0c commit 0bcf795
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/ActionMonitor/Monitors/AcfMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WPGatsby\ActionMonitor\Monitors;

use WPGatsby\Utils\Utils;

class AcfMonitor extends Monitor {

public function init() {
Expand All @@ -28,8 +30,41 @@ function() {
}
);

// @todo: Add support for tracking ACF Options Fields.

add_action('acf/save_post', [$this, 'after_acf_save_post'], 20);
}

/**
* Handles content updates of ACF option pages.
*/
public function after_acf_save_post() {
$option_pages = acf_get_options_pages();

if ( ! is_array( $option_pages ) ) {
return;
}

$option_pages_slugs = array_keys( $option_pages );

/**
* Filters the $option_pages_slugs array.
*
* @since 2.1.2
*
* @param array $option_pages_slugs Array with slugs of all registered ACF option pages.
*/
$option_pages_slugs = apply_filters(
'gatsby_action_monitor_tracked_acf_options_pages',
$option_pages_slugs
);

$screen = get_current_screen();

if(
! empty( $option_pages_slugs )
&& is_array( $option_pages_slugs )
&& Utils::str_in_substr_array( $screen->id, $option_pages_slugs )
) {
$this->trigger_non_node_root_field_update();
}
}
}
27 changes: 27 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace WPGatsby\Utils;

class Utils {

/**
* Checks if any of the strings in $substr_array is a substring in the $haystack.
*
* @since 2.1.2
*
* @param string $haystack
* @param array $substr_array
* @param int $offset
*
* @return bool
*/
public static function str_in_substr_array(string $haystack, array $substr_array, int $offset = 0): bool {
foreach ( $substr_array as $substr ) {
if ($substr && strpos($haystack, $substr, $offset) !== false ) {
return true;
}
}

return false;
}
}

0 comments on commit 0bcf795

Please sign in to comment.