| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if( ! class_exists( 'wp_mapit_map' ) ) { |
|---|
| 4 | /** |
|---|
| 5 | * Class to generate the map from the settings in the post. |
|---|
| 6 | */ |
|---|
| 7 | class wp_mapit_map |
|---|
| 8 | { |
|---|
| 9 | /** |
|---|
| 10 | * Get map latitude |
|---|
| 11 | * |
|---|
| 12 | * @since 1.0 |
|---|
| 13 | * @static |
|---|
| 14 | * @access public |
|---|
| 15 | * @param $postId Int Id of the post |
|---|
| 16 | * @return float Returns the latitude of the map |
|---|
| 17 | */ |
|---|
| 18 | public static function get_map_latitude( $postId ) { |
|---|
| 19 | return trim( get_post_meta( $postId, 'wpmi_map_latitiude', true ) ); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Get map longitude |
|---|
| 24 | * |
|---|
| 25 | * @since 1.0 |
|---|
| 26 | * @static |
|---|
| 27 | * @access public |
|---|
| 28 | * @param $postId Int Id of the post |
|---|
| 29 | * @return float Returns the longitude of the map |
|---|
| 30 | */ |
|---|
| 31 | public static function get_map_longitude( $postId ) { |
|---|
| 32 | return trim( get_post_meta( $postId, 'wpmi_map_longitude', true ) ); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Get map zoom level |
|---|
| 37 | * |
|---|
| 38 | * @since 1.0 |
|---|
| 39 | * @static |
|---|
| 40 | * @access public |
|---|
| 41 | * @param $postId Int Id of the post |
|---|
| 42 | * @return float Returns the zoom level of the map |
|---|
| 43 | */ |
|---|
| 44 | public static function get_map_zoom( $postId ) { |
|---|
| 45 | return trim( get_post_meta( $postId, 'wpmi_map_zoom', true ) ); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Get map display position |
|---|
| 50 | * |
|---|
| 51 | * @since 1.0 |
|---|
| 52 | * @static |
|---|
| 53 | * @access public |
|---|
| 54 | * @param $postId Int Id of the post |
|---|
| 55 | * @return string Returns the display position of the map |
|---|
| 56 | */ |
|---|
| 57 | public static function get_map_position( $postId = null ) { |
|---|
| 58 | if( $postId == null ) { |
|---|
| 59 | global $post; |
|---|
| 60 | |
|---|
| 61 | $postId = $post->ID; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | $mapPosition = trim( get_post_meta( $postId, 'wpmi_map_position', true ) ); |
|---|
| 65 | |
|---|
| 66 | return ( ( $mapPosition != '' ) ? $mapPosition : wp_mapit_admin_settings::get_map_position() ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Get map type |
|---|
| 71 | * |
|---|
| 72 | * @since 1.0 |
|---|
| 73 | * @static |
|---|
| 74 | * @access public |
|---|
| 75 | * @param $postId Int Id of the post |
|---|
| 76 | * @return string Returns the type of the map |
|---|
| 77 | */ |
|---|
| 78 | public static function get_map_type( $postId ) { |
|---|
| 79 | $mapType = trim( get_post_meta( $postId, 'wpmi_map_type', true ) ); |
|---|
| 80 | |
|---|
| 81 | return ( ( $mapType != '' ) ? $mapType : wp_mapit_admin_settings::get_map_type() ); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Get map marker image |
|---|
| 86 | * |
|---|
| 87 | * @since 1.0 |
|---|
| 88 | * @static |
|---|
| 89 | * @access public |
|---|
| 90 | * @param $postId Int Id of the post |
|---|
| 91 | * @return string Returns the marker image of the map |
|---|
| 92 | */ |
|---|
| 93 | public static function get_map_marker( $postId ) { |
|---|
| 94 | $mapMarker = trim( get_post_meta( $postId, 'wpmi_marker_image', true ) ); |
|---|
| 95 | |
|---|
| 96 | return ( ( $mapMarker != '' ) ? $mapMarker : wp_mapit_admin_settings::get_map_marker() ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Get map marker title |
|---|
| 101 | * |
|---|
| 102 | * @since 1.0 |
|---|
| 103 | * @static |
|---|
| 104 | * @access public |
|---|
| 105 | * @param $postId Int Id of the post |
|---|
| 106 | * @return string Returns the marker title of the map |
|---|
| 107 | */ |
|---|
| 108 | public static function get_map_title( $postId ) { |
|---|
| 109 | return trim( get_post_meta( $postId, 'wpmi_marker_title', true ) ); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Get map marker content |
|---|
| 114 | * |
|---|
| 115 | * @since 1.0 |
|---|
| 116 | * @static |
|---|
| 117 | * @access public |
|---|
| 118 | * @param $postId Int Id of the post |
|---|
| 119 | * @return string Returns the marker content of the map |
|---|
| 120 | */ |
|---|
| 121 | public static function get_map_content( $postId ) { |
|---|
| 122 | return nl2br( trim( get_post_meta( $postId, 'wpmi_marker_content', true ) ) ); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Get map marker url |
|---|
| 127 | * |
|---|
| 128 | * @since 1.0 |
|---|
| 129 | * @static |
|---|
| 130 | * @access public |
|---|
| 131 | * @param $postId Int Id of the post |
|---|
| 132 | * @return string Returns the marker url of the map |
|---|
| 133 | */ |
|---|
| 134 | public static function get_map_marker_url( $postId ) { |
|---|
| 135 | return trim( get_post_meta( $postId, 'wpmi_marker_url', true ) ); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Get map width |
|---|
| 140 | * |
|---|
| 141 | * @since 1.0 |
|---|
| 142 | * @static |
|---|
| 143 | * @access public |
|---|
| 144 | * @param $postId Int Id of the post |
|---|
| 145 | * @return int Returns the width of the map |
|---|
| 146 | */ |
|---|
| 147 | public static function get_map_width( $postId ) { |
|---|
| 148 | $width = trim( wp_mapit_admin_settings::get_map_width() ); |
|---|
| 149 | return ( ( $width != '' && intval( $width ) > 0 ) ? $width : 300 ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Get map width type |
|---|
| 154 | * |
|---|
| 155 | * @since 1.0 |
|---|
| 156 | * @static |
|---|
| 157 | * @access public |
|---|
| 158 | * @param $postId Int Id of the post |
|---|
| 159 | * @return string Returns the width type of the map |
|---|
| 160 | */ |
|---|
| 161 | public static function get_map_width_type( $postId ) { |
|---|
| 162 | $widthType = trim( wp_mapit_admin_settings::get_map_width_type() ); |
|---|
| 163 | return ( ( in_array( $widthType, array( 'px', 'per' ) ) ? $widthType : 'per' ) ); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | * Get map height |
|---|
| 168 | * |
|---|
| 169 | * @since 1.0 |
|---|
| 170 | * @static |
|---|
| 171 | * @access public |
|---|
| 172 | * @param $postId Int Id of the post |
|---|
| 173 | * @return int Returns the height of the map |
|---|
| 174 | */ |
|---|
| 175 | public static function get_map_height( $postId ) { |
|---|
| 176 | $height = trim( wp_mapit_admin_settings::get_map_height() ); |
|---|
| 177 | return ( ( $height != '' && intval( $height ) > 0 ) ? $height : 300 ); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Get map height type |
|---|
| 182 | * |
|---|
| 183 | * @since 1.0 |
|---|
| 184 | * @static |
|---|
| 185 | * @access public |
|---|
| 186 | * @param $postId Int Id of the post |
|---|
| 187 | * @return string Returns the height type of the map |
|---|
| 188 | */ |
|---|
| 189 | public static function get_map_height_type( $postId ) { |
|---|
| 190 | $heightType = trim( wp_mapit_admin_settings::get_map_height_type() ); |
|---|
| 191 | return ( ( in_array( $heightType, array( 'px', 'per' ) ) ? $heightType : 'px' ) ); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | * Checks if map is added or not |
|---|
| 196 | * |
|---|
| 197 | * @since 1.0 |
|---|
| 198 | * @static |
|---|
| 199 | * @access public |
|---|
| 200 | * @param $postId Int Id of the post |
|---|
| 201 | * @return boolean Returns true if map is added else false, if no id is passed, current post id is considered |
|---|
| 202 | */ |
|---|
| 203 | public static function has_map( $postId = null ) { |
|---|
| 204 | if ( $postId == null ) { |
|---|
| 205 | global $post; |
|---|
| 206 | |
|---|
| 207 | $postId = $post->ID; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | $lat = self::get_map_latitude( $postId ); |
|---|
| 211 | $lng = self::get_map_longitude( $postId ); |
|---|
| 212 | |
|---|
| 213 | return ( ( $lat != '' && $lat != 0 && $lng != '' && $lng != 0 ) ? true : false ); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * Function to generate the map by id |
|---|
| 218 | * |
|---|
| 219 | * @since 1.0 |
|---|
| 220 | * @static |
|---|
| 221 | * @access public |
|---|
| 222 | * @param $postId Int Id of the post |
|---|
| 223 | * @return string Returns the map's markup as per the map id, if no id is passed, current post id is considered |
|---|
| 224 | */ |
|---|
| 225 | public static function generate_map( $postId = null ) { |
|---|
| 226 | if( $postId == null ) { |
|---|
| 227 | global $post; |
|---|
| 228 | |
|---|
| 229 | $postId = $post->ID; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | ob_start(); |
|---|
| 233 | |
|---|
| 234 | ?> |
|---|
| 235 | <div id="wp_mapit_<?php echo wp_mapit_functions::generate_random_string(); ?>" class="wp_mapit_map" data-lat="<?php echo self::get_map_latitude( $postId ); ?>" data-lng="<?php echo self::get_map_longitude( $postId ); ?>" data-zoom="<?php echo self::get_map_zoom( $postId ); ?>" data-type="<?php echo self::get_map_type( $postId ); ?>" data-marker="<?php echo self::get_map_marker( $postId ) ?>" data-title="<?php echo self::get_map_title( $postId ); ?>" data-content="<?php echo htmlentities( self::get_map_content( $postId ) ); ?>" data-url="<?php echo self::get_map_marker_url( $postId ); ?>" data-width="<?php echo self::get_map_width( $postId ); ?>" data-width-type="<?php echo self::get_map_width_type( $postId ); ?>" data-height="<?php echo self::get_map_height( $postId ); ?>" data-height-type="<?php echo self::get_map_height_type( $postId ); ?>"></div> |
|---|
| 236 | <?php |
|---|
| 237 | |
|---|
| 238 | $content = ob_get_clean(); |
|---|
| 239 | |
|---|
| 240 | return $content; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | } |
|---|