| @@ -1,0 +1,131 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Meta_Box\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Get meta box fields related to posts | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @return array | |
| 17 | + */ | |
| 18 | +function automatorwp_meta_box_options_cb_post_fields( $field ) { | |
| 19 | + | |
| 20 | + // Setup vars | |
| 21 | + $value = $field->escaped_value; | |
| 22 | + $none_value = 'any'; | |
| 23 | + $none_label = __( 'any field', 'automatorwp' ); | |
| 24 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 25 | + | |
| 26 | + if( ! empty( $value ) ) { | |
| 27 | + if( ! is_array( $value ) ) { | |
| 28 | + $value = array( $value ); | |
| 29 | + } | |
| 30 | + | |
| 31 | + foreach( $value as $field_id ) { | |
| 32 | + | |
| 33 | + // Skip option none | |
| 34 | + if( $field_id === $none_value ) { | |
| 35 | + continue; | |
| 36 | + } | |
| 37 | + | |
| 38 | + $options[$field_id] = automatorwp_meta_box_get_field_name( $field_id ); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + return $options; | |
| 43 | + | |
| 44 | +} | |
| 45 | + | |
| 46 | +/** | |
| 47 | + * Get meta box fields related to posts | |
| 48 | + * | |
| 49 | + * @since 1.0.0 | |
| 50 | + * | |
| 51 | + * @return array | |
| 52 | + */ | |
| 53 | +function automatorwp_meta_box_get_post_fields( ) { | |
| 54 | + | |
| 55 | + $options = array(); | |
| 56 | + | |
| 57 | + $post_fields = automatorwp_meta_box_get_all_post_fields(); | |
| 58 | + | |
| 59 | + foreach ( $post_fields as $post_field ) { | |
| 60 | + | |
| 61 | + if ( ! empty( $post_field['id'] ) && ! empty( $post_field['name'] ) ) { | |
| 62 | + | |
| 63 | + $options[] = array( | |
| 64 | + 'id' => $post_field['id'], | |
| 65 | + 'title' => $post_field['name'], | |
| 66 | + ); | |
| 67 | + | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + return $options; | |
| 72 | + | |
| 73 | +} | |
| 74 | + | |
| 75 | +/** | |
| 76 | + * Get meta box field name related to posts | |
| 77 | + * | |
| 78 | + * @since 1.0.0 | |
| 79 | + * | |
| 80 | + * @return array | |
| 81 | + */ | |
| 82 | +function automatorwp_meta_box_get_field_name( $field_id ) { | |
| 83 | + | |
| 84 | + $post_fields = automatorwp_meta_box_get_all_post_fields(); | |
| 85 | + | |
| 86 | + foreach ( $post_fields as $post_field ) { | |
| 87 | + | |
| 88 | + if ( ! empty( $post_field['id'] ) && ! empty( $post_field['name'] ) ) { | |
| 89 | + | |
| 90 | + if ( $post_field['id'] === $field_id ){ | |
| 91 | + $field_name = $post_field['name']; | |
| 92 | + } | |
| 93 | + | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + return $field_name; | |
| 98 | +} | |
| 99 | + | |
| 100 | +/** | |
| 101 | + * Get meta box fields objects related to posts and custom types | |
| 102 | + * | |
| 103 | + * @since 1.0.0 | |
| 104 | + * | |
| 105 | + * @return array | |
| 106 | + */ | |
| 107 | +function automatorwp_meta_box_get_all_post_fields() { | |
| 108 | + | |
| 109 | + if ( ! function_exists( 'rwmb_get_object_fields' ) ) { | |
| 110 | + return array(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + $post_fields = rwmb_get_object_fields( 'post' ); | |
| 114 | + | |
| 115 | + // Get custom types created on Meta Box | |
| 116 | + $custom_post_types = get_posts( [ | |
| 117 | + 'post_type' => 'mb-post-type', | |
| 118 | + 'post_status' => 'publish', | |
| 119 | + 'numberposts' => -1 | |
| 120 | + ] ); | |
| 121 | + | |
| 122 | + if ( !empty ( $custom_post_types ) ) { | |
| 123 | + foreach ( $custom_post_types as $custom_type ) { | |
| 124 | + $custom_fields = rwmb_get_object_fields( $custom_type->post_name ); | |
| 125 | + $post_fields = array_merge($post_fields, $custom_fields); | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + return $post_fields; | |
| 130 | + | |
| 131 | +} | |