| @@ -1,0 +1,150 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\pretty-link\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Get the links | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @return array | |
| 18 | + */ | |
| 19 | +function automatorwp_pretty_link_get_links() { | |
| 20 | + $links = array(); | |
| 21 | + $linksController = new PrliLocalApiController(); | |
| 22 | + $all_links = $linksController->get_all_links(); | |
| 23 | + | |
| 24 | + foreach ($all_links as $link) { | |
| 25 | + $links[] = array( | |
| 26 | + 'id' => $link['id'], | |
| 27 | + 'name' => $link['name'], | |
| 28 | + ); | |
| 29 | + } | |
| 30 | + return $links; | |
| 31 | +} | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Options callback for select fields assigned to links | |
| 35 | + * | |
| 36 | + * @since 1.0.0 | |
| 37 | + * | |
| 38 | + * @param stdClass $field | |
| 39 | + * | |
| 40 | + * @return array | |
| 41 | + */ | |
| 42 | +function automatorwp_pretty_link_options_cb_link( $field ) { | |
| 43 | + | |
| 44 | + // Setup vars | |
| 45 | + $value = $field->escaped_value; | |
| 46 | + $none_value = 'any'; | |
| 47 | + $none_label = __( 'any link', 'automatorwp'); | |
| 48 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label); | |
| 49 | + | |
| 50 | + if( ! empty( $value ) ) { | |
| 51 | + if( ! is_array( $value ) ) { | |
| 52 | + $value = array( $value ); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + | |
| 56 | + foreach( $value as $link_id ) { | |
| 57 | + | |
| 58 | + // Skip option none | |
| 59 | + if( $link_id === $none_value) { | |
| 60 | + continue; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $options[$link_id] = automatorwp_pretty_link_get_link_title( $link_id ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + return $options; | |
| 67 | +} | |
| 68 | + | |
| 69 | +/** | |
| 70 | + * Get link title of specified link id | |
| 71 | + * | |
| 72 | + * @since 1.0.0 | |
| 73 | + * | |
| 74 | + * @param int $link_id | |
| 75 | + * | |
| 76 | + * @return array | |
| 77 | + */ | |
| 78 | +function automatorwp_pretty_link_get_link_title( $link_id ) { | |
| 79 | + | |
| 80 | + // Empty title if no ID provided | |
| 81 | + if( absint( $link_id ) === 0 ) { | |
| 82 | + return ''; | |
| 83 | + } | |
| 84 | + | |
| 85 | + $prliLink = new PrliLink(); | |
| 86 | + $one_link = $prliLink->get_one_by( 'id', $link_id ); | |
| 87 | + $link_data = json_decode(json_encode($one_link), true); | |
| 88 | + $link_name = $link_data['name']; | |
| 89 | + | |
| 90 | + return $link_name; | |
| 91 | + | |
| 92 | +} | |
| 93 | + | |
| 94 | +/** | |
| 95 | + * Get link title of specified link id | |
| 96 | + * | |
| 97 | + * @since 1.0.0 | |
| 98 | + * | |
| 99 | + * @param int $link_id | |
| 100 | + * | |
| 101 | + * @return array | |
| 102 | + */ | |
| 103 | +function automatorwp_pretty_link_get_link_data( $link_id ) { | |
| 104 | + | |
| 105 | + // Empty title if no ID provided | |
| 106 | + if( absint( $link_id ) === 0 ) { | |
| 107 | + return ''; | |
| 108 | + } | |
| 109 | + $prliLink = new PrliLink(); | |
| 110 | + $one_link = $prliLink->get_one_by( 'id', $link_id ); | |
| 111 | + $link_data = json_decode(json_encode($one_link), true); | |
| 112 | + return $link_data; | |
| 113 | + | |
| 114 | +} | |
| 115 | +/** | |
| 116 | + * Generate random slug in case the field is empty | |
| 117 | + * | |
| 118 | + * @since 1.0.0 | |
| 119 | + * | |
| 120 | + * @return String | |
| 121 | + */ | |
| 122 | +function automatorwp_pretty_link_generate_random_slug() { | |
| 123 | + $prliLink = new PrliLink(); | |
| 124 | + return $prliLink->generateValidSlug(); | |
| 125 | +} | |
| 126 | + | |
| 127 | +/** | |
| 128 | + * Options callback for select redirection types of pretty links | |
| 129 | + * | |
| 130 | + * @since 1.0.0 | |
| 131 | + * | |
| 132 | + * @param stdClass $field | |
| 133 | + * | |
| 134 | + * @return array | |
| 135 | + */ | |
| 136 | +function automatorwp_pretty_link_get_all_redirection_options_cb( $field ) { | |
| 137 | + // Setup vars | |
| 138 | + $value = $field->escaped_value; | |
| 139 | + $none_value = 'any'; | |
| 140 | + $none_label = __( 'any points type', 'automatorwp' ); | |
| 141 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 142 | + return array_merge( | |
| 143 | + $options, | |
| 144 | + array( | |
| 145 | + '307' => '307 (Temporary)', | |
| 146 | + '302' => '302 (Temporary)', | |
| 147 | + '301' => '301 (Permanent)' | |
| 148 | + ) | |
| 149 | + ); | |
| 150 | +} | |