| @@ -1,0 +1,100 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Jetpack_CRM\Includes\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Options callback for select2 fields assigned to tags | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param stdClass $field | |
| 17 | + * | |
| 18 | + * @return array | |
| 19 | + */ | |
| 20 | +function automatorwp_jetpack_crm_options_cb_tag( $field ) { | |
| 21 | + | |
| 22 | + // Setup vars | |
| 23 | + $value = $field->escaped_value; | |
| 24 | + $none_value = 'any'; | |
| 25 | + $none_label = __( 'any tag', 'automatorwp' ); | |
| 26 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 27 | + | |
| 28 | + if( ! empty( $value ) ) { | |
| 29 | + if( ! is_array( $value ) ) { | |
| 30 | + $value = array( $value ); | |
| 31 | + } | |
| 32 | + | |
| 33 | + foreach( $value as $tag_id ) { | |
| 34 | + | |
| 35 | + // Skip option none | |
| 36 | + if( $tag_id === $none_value ) { | |
| 37 | + continue; | |
| 38 | + } | |
| 39 | + | |
| 40 | + $options[$tag_id] = automatorwp_jetpack_crm_get_tag_title( $tag_id ); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $options; | |
| 45 | + | |
| 46 | +} | |
| 47 | + | |
| 48 | +/** | |
| 49 | + * Get the tag title | |
| 50 | + * | |
| 51 | + * @since 1.0.0 | |
| 52 | + * | |
| 53 | + * @param int $tag_id | |
| 54 | + * | |
| 55 | + * @return string|null | |
| 56 | + */ | |
| 57 | +function automatorwp_jetpack_crm_get_tag_title( $tag_id ) { | |
| 58 | + | |
| 59 | + // Empty title if no ID provided | |
| 60 | + if( absint( $tag_id ) === 0 ) { | |
| 61 | + return ''; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $class_tags = new zbsDAL; | |
| 65 | + $tag = $class_tags->getTag( $tag_id ); | |
| 66 | + | |
| 67 | + return $tag['name']; | |
| 68 | + | |
| 69 | +} | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Get the tags by type | |
| 73 | + * | |
| 74 | + * @since 1.0.0 | |
| 75 | + * | |
| 76 | + * @param int $type_id ID Type | |
| 77 | + * | |
| 78 | + * @return array | |
| 79 | + */ | |
| 80 | +function automatorwp_jetpack_crm_get_tags ( $type_id ) { | |
| 81 | + | |
| 82 | + $obj_tags = new zbsDAL; | |
| 83 | + $all_tags = $obj_tags->getAllTags( ); | |
| 84 | + | |
| 85 | + foreach ( $all_tags as $tag ){ | |
| 86 | + | |
| 87 | + if ( $tag['objtype'] === (string) $type_id ){ | |
| 88 | + | |
| 89 | + $tags[] = array( | |
| 90 | + 'id' => $tag['id'], | |
| 91 | + 'name' => $tag['name'] | |
| 92 | + ); | |
| 93 | + | |
| 94 | + } | |
| 95 | + | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $tags; | |
| 99 | + | |
| 100 | +} | |