protected function formatted_items($type)
 {
     $formatted_items = [];
     switch ($type) {
         case 'tree-selection':
             foreach ($this->items as $tree) {
                 $attachment_id = get_post_thumbnail_id($tree->ID);
                 $img_thumb_url = wp_get_attachment_image_src($attachment_id, 'selection-thumb');
                 $img_url = wp_get_attachment_image_url($attachment_id, 'full');
                 $formatted_items[] = array('ID' => ForestUtils::get_instance()->get_js_object(array('ID' => $tree->ID, 'title' => $tree->post_title), true), 'post_title' => $tree->post_title, 'img' => sprintf('<a href="%s" class="thickbox"><img src="%s" style="width:%dpx;height:%dpx"/></a>', $img_url, $img_thumb_url[0], $img_thumb_url[1], $img_thumb_url[2]));
             }
             break;
     }
     return $formatted_items;
 }
        public function get_modal()
        {
            ?>
			<!-- Modal Dialog-->
			<div class="modal fade" id="<?php 
            echo $this->id;
            ?>
" tabindex="-1" role="dialog" aria-labelledby="add-title-label">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close">
								<span aria-hidden="true">&times;</span>
							</button>
							<h4 class="modal-title" id="add-title-label">
								<?php 
            echo $this->title;
            ?>
							</h4>
						</div>

						<div class="modal-body">

							<div class="filter-box bg-info form-inline <?php 
            echo $this->has_filter ? '' : 'hidden';
            ?>
">

								<?php 
            do_action('modal_post_selection_filter');
            ?>

								<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-filter"></i></button>

							</div>

							<div class="plan-items">

								<table class="post-type-table table table-hover table-condensed table-striped" data-chk-id="chk_item_select">

									<caption>
										<div class="river-waiting">
											<span class="spinner"></span>
										</div>
										<em><?php 
            _etfc('Loading...');
            ?>
</em>
									</caption>

									<thead>
										<?php 
            $this->get_column_headers($this->columns, $this->sort_by);
            ?>
									</thead>

									<tbody>
										<?php 
            $rows = apply_filters('get_modal_post_selection_rows', $this->current_page);
            if (!isset($this->columns) || !is_array($this->columns) || count($this->columns) < 1) {
                throw new Exception('missing column definition');
            }
            if (!isset($rows) || !is_array($rows) || count($rows) == 0) {
                ?>
												<tr>
													<td colspan="<?php 
                echo $this->get_column_count();
                ?>
"
													    class="message no-items"><?php 
                _e('No items', 'forest-manager');
                ?>
													</td>
												</tr>

												<?php 
            } else {
                foreach ($rows as $row) {
                    echo '<tr>';
                    echo '<td><input type="radio" name="chk_item_select" value="' . $row[$this->row_id] . '"/></td>';
                    foreach ($this->columns as $column_id => $column_title) {
                        echo '<td>' . $row[$column_id] . '</td>';
                    }
                    echo '</tr>';
                }
            }
            ?>
									</tbody>
									<tfoot>
									<tr>
										<td colspan="<?php 
            echo count($this->columns);
            ?>
">
											<div class="pagination-container">
												<?php 
            $this->get_bootstrap_pagination();
            ?>
											</div>
										</td>
									</tr>
									</tfoot>
								</table>

							</div>

						</div><!--modal body end-->

						<div class="modal-footer">
							<button type="button" class="btn btn-default"
							        data-dismiss="modal"><?php 
            _etfm('Close');
            ?>
</button>
							<button type="button"
							        class="btn btn-primary"><?php 
            _etfm('Select');
            ?>
</button>
						</div>
					</div>
				</div>
			</div>

			<?php 
            $labels = array('NoItemsFound' => _tfc('No items found'));
            ?>

			<script type="application/javascript">
				jQuery(document).ready(function(){
					try {
						var modalSelection = new ForestModalPostSelection(
							'<?php 
            echo $this->id;
            ?>
',
							<?php 
            echo ForestUtils::get_instance()->get_js_object($this->post_types);
            ?>
,
							<?php 
            echo ForestUtils::get_instance()->get_js_object($this->sort_by);
            ?>
,
							<?php 
            echo $this->has_filter ? 'true' : 'false';
            ?>
,
							<?php 
            echo ForestUtils::get_instance()->get_js_object($this->columns);
            ?>
,
							<?php 
            echo ForestUtils::get_instance()->get_js_object($this->ajax);
            ?>
,
							<?php 
            echo ForestUtils::get_instance()->get_js_object($labels);
            ?>
						);
						modalSelection.init();
					}
					catch(e){
						console.log(e.message);
					}
				});
			</script>

<?php 
        }
 /**
  * This method excepts a two member array ($value[0]) the first is the post fields and the second ($value[1])
  * is the meta fields
  * @param $values
  *
  * @return bool|int
  */
 public function update_post($id, $values)
 {
     if (!isset($values) || !is_array($values)) {
         $this->create_error('Invalid value parameter in update_post');
         return false;
     }
     global $wp_version;
     $wp_ver_int = explode('.', $wp_version);
     if ((int) $wp_ver_int[0] >= 4 && (int) $wp_ver_int[1] >= 4) {
         $post_data = $values[0];
         if (count($values) > 1) {
             $post_data['meta_input'] = $values[1];
         }
         //first object/array is post data
         $result = wp_update_post($post_data, true);
         if ($this->has_errors($result)) {
             return false;
         } else {
             return $result;
         }
     } else {
         $result = wp_update_post($values[0], true);
         if ($this->has_errors($result)) {
             return false;
         }
         //the second meta data
         $post_meta_data = ForestUtils::get_instance()->objectToArray($values[1]);
         foreach ($post_meta_data as $meta_key => $meta_value) {
             if (!update_post_meta($id, $meta_key, $meta_value)) {
                 if ($this->has_errors($result)) {
                     return false;
                 }
                 if (!add_post_meta($id, $meta_key, $meta_value)) {
                     $this->has_errors($result);
                     return false;
                 }
             }
         }
     }
     return true;
 }
 /**
  * When storing json strigns in the db the return value cannot be stored in a value html field because it breaks the
  * html sequance with the double quotes. I remove the quotes.
  * @param $key
  * @param $meta_values
  *
  * @return string
  */
 private function get_meta_value($key, $meta_values)
 {
     if (array_key_exists($key, $meta_values)) {
         if (is_array($meta_values[$key])) {
             return ForestUtils::get_instance()->get_html_safe($meta_values[$key][0]);
         } else {
             return ForestUtils::get_instance()->get_html_safe($meta_values[$key]);
         }
     } else {
         return '';
     }
 }
 public function save_tree_card()
 {
     //check nounce and clear buffer
     $data = $this->start_ajax('tree_card_metabox', '_tree-card-nounce');
     try {
         $arguments = array_key_exists('data', $_POST) ? $_POST['data'] : null;
         if ($arguments) {
             $arguments = ForestUtils::get_instance()->json_decode($arguments);
             $arguments = $this->parse_tree_args($arguments);
             $tree_card_id = $arguments['tree_card_id'];
             //2 stdClasses
             $values = $arguments['values'];
             for ($i = 0; $i < count($values); $i++) {
                 if (is_object($values[$i])) {
                     $values[$i] = ForestUtils::get_instance()->objectToArray($values[$i]);
                 }
             }
             $query = new TreeCardDbQuery($tree_card_id);
             $query_result = $query->update_tree_card($values);
             if ($query_result) {
                 $data['message'] = _tfm('Tree card updated');
                 $data['type'] = 'success';
                 $data['action'] = $this->get_ajax_action(__FUNCTION__);
                 //has to match the html tab id
             } else {
                 $data['message'] = _tfm('Tree Card was not updated:') . $query->errors;
                 $data['type'] = 'error';
                 $data['action'] = $this->get_ajax_action(__FUNCTION__);
             }
         } else {
             $data['type'] = 'error';
             $data['message'] = _tfm('Error saving plan');
         }
     } catch (Exception $e) {
         $data['type'] = 'error';
         $data['message'] = _tfm('Error saving plan. Server error was:') . $e->getMessage();
     }
     //add error bufferd to data and json encode
     echo $this->end_ajax($data);
     die;
     // this is required to return a proper result
 }