=== Safe Function Call === Contributors: coffee2code Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6ARCFJ9TX3522 Tags: function, template, plugin, error, coffee2code License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Requires at least: 1.5 Tested up to: 5.7 Stable tag: 1.3 Safely and easily call functions that may not be available (such as those provided by a plugin that gets deactivated) == Description == Safely call a function, class method, or object method in a manner that doesn't generate errors if those plugins cease to exist. Various helper functions are provided that provide handy variations of this theme: * `_sfc()`: Safely call a function and get its return value * `_sfce()`: Safely call a function and echo its return value * `_sfcf()`: Safely call a function; if it doesn't exist, then a fallback function (if specified) is called * `_sfcm()`: Safely call a function; if it doesn't exist, then echo a message (if provided) Let's assume you had something like this in a template: `` If you deactivated the plugin that provided `list_cities()`, your site would generate an error when that template is accessed. You can instead use `_sfc()`, which is provided by this plugin to call other functions, like so: `` That will simply do nothing if the `list_cities()` function is not available. If you'd rather display a message when the function does not exist, use `_sfcm()` instead, like so: `` In this case, if `list_cities()` is not available, the text "The cities listing is temporarily disabled." will be displayed. If you'd rather call another function when the function does not exist, use _sfcf() instead, like so: `` In the event you want to safely call a function and echo its value, you can use `_sfce()` like so: `` Which is roughly equivalent to doing : `` = Filter invocation method = To further prevent issues in your code should this plugin itself become deactivated, you can use indirect filter invocation to call the plugin functions. Each function has an associated filter with the same name as the function. Simply use `apply_filters()` to invoke that function instead of calling the function directly. E.g. instead of: `` Do: `` If you're relying on the return value of a function and this plugin gets deactivated, note that the `apply_filters()` call will return the name of the function you intended to call, so you should check the return value to ensure the function got called. Instead of: `` Do: `` Links: [Plugin Homepage](https://coffee2code.com/wp-plugins/safe-function-call/) | [Plugin Directory Page](https://wordpress.org/plugins/safe-function-call/) | [GitHub](https://github.com/coffee2code/safe-function-call/) | [Author Homepage](https://coffee2code.com) == Installation == 1. Install via the built-in WordPress plugin installer. Or download and unzip `safe-function-call.zip` inside the plugins directory for your site (typically `wp-content/plugins/`) 1. Activate the plugin through the 'Plugins' admin menu in WordPress 1. Use any of the four functions provided by this plugin as desired == Frequently Asked Questions == = Do the functions provided by this plugin capture any error messages generated by the specified function? = No. = Why would I use any of these functions instead of using `function_exists()`/`method_exists()` directly? = The functions provided by this plugin provide a more concise syntax for checking for function existence (but they do use `function_exists()`/`method_exists()` under the hood). `_sfce()` will both echo and return the echoed value, which may be of use in certain circumstances. And also, since the callback to be safely called is passed as an argument, it can be easily and more concisely parameterized. = Won't the problems this plugin addresses become a problem when using this plugin if it itself gets deactivated? = Yes, if you make direct use of one of this plugin's functions and then deactivate the plugin, you will likely encounter an error. However, if you make use indirect filter invocation, you can prevent errors. See the "Filter invocation method" section of the extended plugin description for example code. = Does this plugin include unit tests? = Yes. == Template Tags == The plugin provides four functions for your use. *Note: These functions are not limited to use in templates* = Functions = * `` This will safely invoke the specified callback. You can specify an arbitrary number of additional arguments that will get passed to it. If the callback does not exist, nothing is displayed and no error is generated. * `` The same as `_sfc()` except that it echoes the return value of the callback before returning that value. * `` The same as `_sfc()` except that it invokes the fallback callback (if it exists) if the callback does not exist. `$function_name_if_missing()` is sent `$function_name` as its first argument, and then subsequently all arguments that would have otherwise been sent to `$function_name()`. * `` The same as `_sfc()` except that it displays a message (the value of `$message_if_missing`), if the callback does not exist. = Arguments = * `$callback` A string representing the name of the function to be called, or an array of a class or object and its method (as can be done for `add_action()`/`add_filter()`) * `$message_if_missing` (For `_sfcm()` only.) The message to be displayed if `$function_name()` does not exist as a function. * `$fallback_callback` (For `_sfcf()` only.) The function to be called if the callback does not exist. = Examples = * `` "Austin, Dallas, Fort Worth" * `` "Austin, Dallas, Fort Worth" * `` "Austin, Dallas, Fort Worth" * `` "" * `` "Austin, Dallas, Fort Worth" * `` "Unable to list cities at the moment" * `` "Houston" * `` == Changelog == = 1.3 (2021-04-17) = Highlights: * This minor release adds support for a safer method of invoking the plugin's own functions in a way that safeguards your usage against errors if the plugin gets deactivated and also notes compatibility through WP 5.7+. Details: * New: Support filter invocation for all functions * Add filter `_sfc` to support filter invocation method `_sfc()` * Add filter `_sfce` to support filter invocation method `_sfce()` * Add filter `_sfcf` to support filter invocation method `_sfcf()` * Add filter `_sfcm` to support filter invocation method `_sfcm()` * Change: Fix incorrect function docblock description and remove repeated word in some parameter docblocks * Change: Note compatibility through WP 5.7+ * Change: Update copyright date (2021) = 1.2.11 (2020-09-09) = * Change: Restructure unit test file structure * New: Create new subdirectory `phpunit/` to house all files related to unit testing * Change: Move `bin/` to `phpunit/bin/` * Change: Move `tests/bootstrap.php` to `phpunit/` * Change: Move `tests/` to `phpunit/tests/` * Change: Rename `phpunit.xml` to `phpunit.xml.dist` per best practices * Change: Note compatibility through WP 5.5+ = 1.2.10 (2020-04-30) = * Change: Use HTTPS for link to WP SVN repository in bin script for configuring unit tests * Change: Note compatibility through WP 5.4+ * Change: Update links to coffee2code.com to be HTTPS _Full changelog is available in [CHANGELOG.md](https://github.com/coffee2code/safe-function-call/blob/master/CHANGELOG.md)._ == Upgrade Notice == = 1.3 = Minor update: added support for a safer method of invoking the plugin's own functions in a way that safeguards your usage against errors if the plugin gets deactivated, noted compatibility through WP 5.7+., and updated copyright date (2021). = 1.2.11 = Trivial update: Restructured unit test file structure and noted compatibility through WP 5.5+. = 1.2.10 = Trivial update: Updated a few URLs to be HTTPS and noted compatibility through WP 5.4+. = 1.2.9 = Trivial update: noted compatibility through WP 5.3+ and updated copyright date (2020) = 1.2.8 = Trivial update: modernized unit tests, noted compatibility through WP 5.2+ = 1.2.7 = Trivial update: created CHANGELOG.md to store historical changelog outside of readme.txt, noted compatibility through WP 5.1+, and updated copyright date (2019) = 1.2.6 = Trivial update: made script usable outside of PHP, added README.md and GitHub link, noted compatibility through WP 4.9+, and updated copyright date = 1.2.5 = Trivial update: tweaked readme, changed unit test bootstrap, noted compatibility through WP 4.7+, and updated copyright date = 1.2.4 = Trivial update: noted compatibility through WP 4.4+ and updated copyright date = 1.2.3 = Trivial update: noted compatibility through WP 4.3+ = 1.2.2 = Trivial update: noted compatibility through WP 4.1+ and updated copyright date = 1.2.1 = Trivial update: noted compatibility through WP 4.0+; added plugin icon. = 1.2 = Recommended update: added support for full callback usage; improved documentation; added unit tests; noted compatibility through WP 3.8+ = 1.1.7 = Trivial update: noted compatibility through WP 3.5+ = 1.1.6 = Trivial update: noted compatibility through WP 3.4+; explicitly stated license = 1.1.5 = Trivial update: noted compatibility through WP 3.3+ and minor code documentation formatting changes (spacing) = 1.1.4 = Trivial update: noted compatibility through WP 3.2+ and minor code formatting changes (spacing) = 1.1.3 = Trivial update: documentation tweaks = 1.1.2 = Trivial update: noted compatibility through WP 3.1+ and updated copyright date = 1.1.1 = Minor update. Wrapped functions in if(function_exists()) checked; noted compatibility with WP 3.0+.