| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Code_Snippets\Functions |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get snippets from Code Snippets |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param stdClass $field |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function automatorwp_code_snippets_options_cb_snippet( $field ) { |
| 22 |
|
| 23 |
// Setup vars |
| 24 |
$value = $field->escaped_value; |
| 25 |
$none_value = 'any'; |
| 26 |
$none_label = __( 'any snippet', 'automatorwp' ); |
| 27 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 28 |
|
| 29 |
if( ! empty( $value ) ) { |
| 30 |
if( ! is_array( $value ) ) { |
| 31 |
$value = array( $value ); |
| 32 |
} |
| 33 |
|
| 34 |
foreach( $value as $snippet_id ) { |
| 35 |
|
| 36 |
// Skip option none |
| 37 |
if( $snippet_id === $none_value ) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
$options[$snippet_id] = automatorwp_code_snippets_get_snippet_name( $snippet_id ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return $options; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get snippets from Code Snippets |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function automatorwp_code_snippets_get_snippets( ) { |
| 58 |
|
| 59 |
$snippets = array(); |
| 60 |
|
| 61 |
$all_snippets = Code_Snippets\get_snippets(); |
| 62 |
|
| 63 |
foreach ( $all_snippets as $snippet ){ |
| 64 |
|
| 65 |
$snippets[] = array( |
| 66 |
'id' => $snippet->id, |
| 67 |
'name' => $snippet->name, |
| 68 |
); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
return $snippets; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get snippet name |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param int $snippet_id ID snippet |
| 82 |
* |
| 83 |
*/ |
| 84 |
function automatorwp_code_snippets_get_snippet_name( $snippet_id ) { |
| 85 |
|
| 86 |
if( ! $snippet_id ) { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
$snippet = Code_Snippets\get_snippet( $snippet_id ); |
| 91 |
|
| 92 |
return $snippet->name; |
| 93 |
|
| 94 |
} |