| @@ -1,0 +1,151 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\wpForo\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 forums | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param stdClass $field | |
| 17 | + * | |
| 18 | + * @return array | |
| 19 | + */ | |
| 20 | +function automatorwp_wpforo_options_cb_forum( $field ) { | |
| 21 | + | |
| 22 | + // Setup vars | |
| 23 | + $value = $field->escaped_value; | |
| 24 | + $none_value = 'any'; | |
| 25 | + $none_label = __( 'any forum', 'automatorwp-wpforo' ); | |
| 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 $forum_id ) { | |
| 34 | + | |
| 35 | + // Skip option none | |
| 36 | + if( $forum_id === $none_value ) { | |
| 37 | + continue; | |
| 38 | + } | |
| 39 | + | |
| 40 | + $options[$forum_id] = automatorwp_wpforo_get_forum_title( $forum_id ); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $options; | |
| 45 | + | |
| 46 | +} | |
| 47 | + | |
| 48 | +/** | |
| 49 | + * Get the forum title | |
| 50 | + * | |
| 51 | + * @since 1.0.0 | |
| 52 | + * | |
| 53 | + * @param int $forum_id | |
| 54 | + * | |
| 55 | + * @return string|null | |
| 56 | + */ | |
| 57 | +function automatorwp_wpforo_get_forum_title( $forum_id ) { | |
| 58 | + | |
| 59 | + // Empty title if no ID provided | |
| 60 | + if( $forum_id === 0 ) { | |
| 61 | + return ''; | |
| 62 | + } | |
| 63 | + | |
| 64 | + global $wpdb; | |
| 65 | + | |
| 66 | + if ( strpos( $forum_id, '-' ) ) { | |
| 67 | + $board = explode('-', $forum_id)[0]; | |
| 68 | + $forum_id = explode('-', $forum_id)[1]; | |
| 69 | + $table = $table = $wpdb->prefix . 'wpforo_' . $board . '_forums'; | |
| 70 | + | |
| 71 | + } else { | |
| 72 | + $table = WPF()->tables->forums; | |
| 73 | + } | |
| 74 | + | |
| 75 | + return $wpdb->get_var( $wpdb->prepare( | |
| 76 | + "SELECT f.title FROM {$table} AS f WHERE f.forumid = %d", | |
| 77 | + $forum_id | |
| 78 | + ) ); | |
| 79 | + | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Options callback for select2 fields assigned to topics | |
| 84 | + * | |
| 85 | + * @since 1.0.0 | |
| 86 | + * | |
| 87 | + * @param stdClass $field | |
| 88 | + * | |
| 89 | + * @return array | |
| 90 | + */ | |
| 91 | +function automatorwp_wpforo_options_cb_topic( $field ) { | |
| 92 | + | |
| 93 | + // Setup vars | |
| 94 | + $value = $field->escaped_value; | |
| 95 | + $none_value = 'any'; | |
| 96 | + $none_label = __( 'any topic', 'automatorwp-wpforo' ); | |
| 97 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 98 | + | |
| 99 | + if( ! empty( $value ) ) { | |
| 100 | + if( ! is_array( $value ) ) { | |
| 101 | + $value = array( $value ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + foreach( $value as $topic_id ) { | |
| 105 | + | |
| 106 | + // Skip option none | |
| 107 | + if( $topic_id === $none_value ) { | |
| 108 | + continue; | |
| 109 | + } | |
| 110 | + | |
| 111 | + $options[$topic_id] = automatorwp_wpforo_get_topic_title( $topic_id ); | |
| 112 | + } | |
| 113 | + } | |
| 114 | + | |
| 115 | + return $options; | |
| 116 | + | |
| 117 | +} | |
| 118 | + | |
| 119 | +/** | |
| 120 | + * Get the topic title | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + * | |
| 124 | + * @param int $topic_id | |
| 125 | + * | |
| 126 | + * @return string|null | |
| 127 | + */ | |
| 128 | +function automatorwp_wpforo_get_topic_title( $topic_id ) { | |
| 129 | + | |
| 130 | + // Empty title if no ID provided | |
| 131 | + if( absint( $topic_id ) === 0 ) { | |
| 132 | + return ''; | |
| 133 | + } | |
| 134 | + | |
| 135 | + global $wpdb; | |
| 136 | + | |
| 137 | + if ( strpos( $topic_id, '-' ) ) { | |
| 138 | + $board = explode('-', $topic_id)[0]; | |
| 139 | + $topic_id = explode('-', $topic_id)[1]; | |
| 140 | + $table = $table = $wpdb->prefix . 'wpforo_' . $board . '_topics'; | |
| 141 | + | |
| 142 | + } else { | |
| 143 | + $table = WPF()->tables->topics; | |
| 144 | + } | |
| 145 | + | |
| 146 | + return $wpdb->get_var( $wpdb->prepare( | |
| 147 | + "SELECT f.title FROM {$table} AS f WHERE f.topicid = %d", | |
| 148 | + $topic_id | |
| 149 | + ) ); | |
| 150 | + | |
| 151 | +} | |