| @@ -1,0 +1,99 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WPCode\Functions | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Get snippets from WPCode | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param stdClass $field | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function automatorwp_wpcode_options_cb_snippet( $field ) { | |
| 22 | + | |
| 23 | + // Setup vars | |
| 24 | + $value = $field->escaped_value; | |
| 25 | + $none_value = 'any'; | |
| 26 | + $none_label = __( 'any snippet', 'automatorwp-wpcode' ); | |
| 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_wpcode_get_snippet_name( $snippet_id ); | |
| 42 | + } | |
| 43 | + } | |
| 44 | + | |
| 45 | + return $options; | |
| 46 | + | |
| 47 | +} | |
| 48 | + | |
| 49 | +/** | |
| 50 | +* Get snippets from WPCode | |
| 51 | +* | |
| 52 | +* @since 1.0.0 | |
| 53 | +* | |
| 54 | +* | |
| 55 | +* @return array | |
| 56 | +*/ | |
| 57 | +function automatorwp_wpcode_get_snippets( ) { | |
| 58 | + | |
| 59 | + $snippets = array(); | |
| 60 | + | |
| 61 | + $query = new WP_Query( array( | |
| 62 | + 'post_type' => 'wpcode', | |
| 63 | + 'post_status' => 'draft,publish', | |
| 64 | + 'fields' => 'ids', | |
| 65 | + 'posts_per_page' => -1, | |
| 66 | + ) ); | |
| 67 | + | |
| 68 | + $all_snippets = $query->get_posts(); | |
| 69 | + | |
| 70 | + foreach ( $all_snippets as $snippet_id ){ | |
| 71 | + | |
| 72 | + $snippets[] = array( | |
| 73 | + 'id' => $snippet_id, | |
| 74 | + 'name' => get_the_title( $snippet_id ), | |
| 75 | + ); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + return $snippets; | |
| 80 | + | |
| 81 | +} | |
| 82 | + | |
| 83 | +/** | |
| 84 | +* Get snippet name | |
| 85 | +* | |
| 86 | +* @since 1.0.0 | |
| 87 | +* | |
| 88 | +* @param int $snippet_id ID snippet | |
| 89 | +* | |
| 90 | +*/ | |
| 91 | +function automatorwp_wpcode_get_snippet_name( $snippet_id ) { | |
| 92 | + | |
| 93 | + if( ! $snippet_id ) { | |
| 94 | + return ''; | |
| 95 | + } | |
| 96 | + | |
| 97 | + return get_the_title( $snippet_id ); | |
| 98 | + | |
| 99 | +} | |