birch_defn($ns, 'init', function () use($ns, $birchschedule) { birch_defmethod($birchschedule->model, 'pre_save', 'birs_staff', $ns->pre_save); birch_defmethod($birchschedule->model, 'post_get', 'birs_staff', $ns->post_get); }); birch_defn($ns, 'pre_save', function ($staff, $config) { birch_assert(is_array($staff) && isset($staff['post_type'])); if (isset($staff['_birs_assigned_services'])) { $staff['_birs_assigned_services'] = serialize($staff['_birs_assigned_services']); } if (isset($staff['_birs_staff_schedule'])) { $staff['_birs_staff_schedule'] = serialize($staff['_birs_staff_schedule']); } return $staff; }); birch_defn($ns, 'post_get', function ($staff) { birch_assert(is_array($staff) && isset($staff['post_type'])); if (isset($staff['post_title'])) { $staff['_birs_staff_name'] = $staff['post_title']; } if (isset($staff['_birs_assigned_services'])) { $assigned_services = $staff['_birs_assigned_services']; $assigned_services = unserialize($assigned_services); $assigned_services = $assigned_services ? $assigned_services : array(); $staff['_birs_assigned_services'] = $assigned_services; } if (isset($staff['_birs_staff_schedule'])) { $schedule = $staff['_birs_staff_schedule']; if (!isset($schedule)) { $schedule = array(); } else { $schedule = unserialize($schedule);
birch_ns('birchschedule.model.cpt.client', function ($ns) { global $birchschedule; birch_defn($ns, 'init', function () use($ns, $birchschedule) { birch_defmethod($birchschedule->model, 'pre_save', 'birs_client', $ns->pre_save); birch_defmethod($birchschedule->model, 'save', 'birs_client', $ns->save); birch_defmethod($birchschedule->model, 'post_get', 'birs_client', $ns->post_get); }); birch_defn($ns, 'pre_save', function ($client, $config) { birch_assert(is_array($client) && isset($client['post_type'])); $name_first = ''; $name_last = ''; if (isset($client['_birs_client_name_first'])) { $name_first = $client['_birs_client_name_first']; } if (isset($client['_birs_client_name_last'])) { $name_last = $client['_birs_client_name_last']; } $client['post_title'] = $name_first . ' ' . $name_last; return $client; }); birch_defn($ns, 'save', function ($client, $config) use($ns, $birchschedule) { return $birchschedule->model->save->fns['_root']($client, $config); }); birch_defn($ns, 'post_get', function ($client) { birch_assert(is_array($client) && isset($client['post_type'])); if (isset($client['_birs_client_name_first']) && isset($client['_birs_client_name_last'])) { $client['_birs_client_name'] = $client['_birs_client_name_first'] . ' ' . $client['_birs_client_name_last']; } return $client; }); });
$meta_keys = $config['meta_keys']; foreach ($meta_keys as $key) { $model[$key] = get_post_meta($id, $key, true); } return $model; }); birch_defn($ns, 'is_valid_id', function ($id) use($ns) { return (string) (int) $id == $id && $id > 0; }); birch_defn($ns, 'delete', function ($id) use($ns) { birch_assert($ns->is_valid_id($id)); return wp_delete_post($id); }); birch_defn($ns, 'save', function ($model, $config) use($ns) { birch_assert(is_array($model), 'Model should be an array.'); birch_assert(isset($model['post_type']), 'Model should have post_type field.'); global $birchpress; $config = $ns->_preprocess_config($config); $base_keys = $config['base_keys']; $meta_keys = $config['meta_keys']; $id = 0; if (isset($model['ID'])) { if ($ns->is_valid_id($model['ID'])) { $id = $model['ID']; } else { unset($model['ID']); } } $model_fields = array_keys($model); foreach ($model_fields as $field) { if (!in_array($field, $base_keys) && !in_array($field, $meta_keys) && $field != 'ID' && $field != 'post_type') {
echo $error_id; ?> "><?php echo $message; ?> </div> <?php } ?> </div> <?php exit; }); birch_defn($ns, 'render_meta_boxes', function ($config) use($ns) { $assert_criteria = is_array($config) && isset($config['screen']); birch_assert($assert_criteria); $default_config = array('cols' => 1, 'callback_arg' => ''); $config = array_merge($default_config, $config); if ($config['cols'] === 2) { ?> <div id="poststuff"> <div id="post-body" class="metabox-holder columns-2"> <div id="postbox-container-1" class="postbox-container"> <?php do_meta_boxes($config['screen'], 'side', $config['callback_arg']); ?> </div> <div id="postbox-container-2" class="postbox-container"> <?php do_meta_boxes($config['screen'], 'normal', $config['callback_arg']); ?>
} return false; }; $ns->get_client_fields = function () { $client_meta_keys = array('_birs_client_name_first', '_birs_client_name_last', '_birs_client_email', '_birs_client_phone', '_birs_client_address1', '_birs_client_address2', '_birs_client_city', '_birs_client_state', '_birs_client_province', '_birs_client_country', '_birs_client_zip'); return $client_meta_keys; }; $ns->get_payment_fields = function () { return array('_birs_payment_appointment', '_birs_payment_client', '_birs_payment_amount', '_birs_payment_type', '_birs_payment_trid', '_birs_payment_notes', '_birs_payment_timestamp', '_birs_payment_currency', '_birs_payment_3rd_txn_id'); }; $ns->get_meta_key_label = function ($meta_key) { return ''; }; $ns->get_services_by_location = function ($location_id) { global $birchschedule; birch_assert($birchschedule->model->is_valid_id($location_id)); $location = array('ID' => $location_id); $services = $birchschedule->model->query(array('post_type' => 'birs_service', 'order' => 'ASC', 'orderby' => 'title'), array('meta_keys' => array('_birs_service_assigned_locations'), 'base_keys' => array('post_title'))); $assigned_services = array(); foreach ($services as $service) { $assigned_locations = $service['_birs_service_assigned_locations']; if ($assigned_locations) { if (isset($assigned_locations[$location_id])) { $assigned_services[$service['ID']] = $service['post_title']; } } } return $assigned_services; }; $ns->get_staff_by_location = function ($location_id) { global $birchschedule;
<?php birch_ns('birchschedule.model.cpt.location', function ($ns) { global $birchschedule; birch_defn($ns, 'init', function () use($ns, $birchschedule) { birch_defmethod($birchschedule->model, 'pre_save', 'birs_location', $ns->pre_save); birch_defmethod($birchschedule->model, 'post_get', 'birs_location', $ns->post_get); }); birch_defn($ns, 'pre_save', function ($location, $config) { birch_assert(is_array($location) && isset($location['post_type'])); return $location; }); birch_defn($ns, 'post_get', function ($location) { birch_assert(is_array($location) && isset($location['post_type'])); if (isset($location['post_title'])) { $location['_birs_location_name'] = $location['post_title']; } if (isset($location['post_content'])) { $location['_birs_location_description'] = $location['post_content']; } return $location; }); });
function birch_ns($ns_name, $init_func = false) { birch_assert(is_string($ns_name), sprintf('The namespace <%s> should be string.', $ns_name)); $ns = explode('.', $ns_name); $current_str = $ns[0]; if (isset($GLOBALS[$current_str])) { $GLOBALS[$current_str] = _birch_create_ns($current_str, $GLOBALS[$current_str]); } else { $GLOBALS[$current_str] = _birch_create_ns($current_str, false); } $current = $GLOBALS[$current_str]; $subs = array_slice($ns, 1); foreach ($subs as $sub) { $current_str .= '.' . $sub; if (isset($current[$sub])) { $current[$sub] = _birch_create_ns($current_str, $current[$sub]); } else { $current[$sub] = _birch_create_ns($current_str, false); } $current = $current[$sub]; } if (is_callable($init_func)) { global $birch_current_ns; $birch_current_ns = $current; $init_func($current); $birch_current_ns = false; } return $current; }
}); birch_defn($ns, 'reschedule_appointment', function ($appointment_id, $appointment_info) use($ns) { birch_assert(isset($appointment_info['_birs_appointment_staff']) || isset($appointment_info['_birs_appointment_timestamp'])); global $birchpress, $birchschedule; $appointment1on1s = $ns->get_appointment1on1s_by_appointment($appointment_id, array('status' => 'publish')); if ($appointment1on1s) { foreach ($appointment1on1s as $appointment1on1_id => $appointment1on1) { $ns->reschedule_appointment1on1($appointment1on1_id, $appointment_info); } } }); birch_defn($ns, 'do_reschedule_appointment1on1', function ($appointment1on1_id, $appointment_info) { }); birch_defn($ns, 'reschedule_appointment1on1', function ($appointment1on1_id, $appointment_info) use($ns) { global $birchpress, $birchschedule; birch_assert(isset($appointment_info['_birs_appointment_staff']) || isset($appointment_info['_birs_appointment_timestamp'])); $appointment1on1 = $birchschedule->model->get($appointment1on1_id, array('base_keys' => array(), 'meta_keys' => array('_birs_appointment_id', '_birs_client_id'))); if (!$appointment1on1) { return $birchpress->util->new_error('appointment1on1_nonexist', __('The appointment does not exist.', 'birchschedule')); } $appointment = $birchschedule->model->get($appointment1on1['_birs_appointment_id'], array('base_keys' => array(), 'meta_keys' => array('_birs_appointment_location', '_birs_appointment_service', '_birs_appointment_staff', '_birs_appointment_timestamp', '_birs_appointment_duration', '_birs_appointment_padding_before', '_birs_appointment_padding_after'))); if (!$appointment) { return $birchpress->util->new_error('appointment_nonexist', __('The appointment does not exist.', 'birchschedule')); } if (!isset($appointment_info['_birs_appointment_staff'])) { $appointment_info['_birs_appointment_staff'] = $appointment['_birs_appointment_staff']; } if (!isset($appointment_info['_birs_appointment_timestamp'])) { $appointment_info['_birs_appointment_timestamp'] = $appointment['_birs_appointment_timestamp']; } if (!isset($appointment_info['_birs_appointment_location'])) {
birch_ns('birchschedule.model.cpt.appointment', function ($ns) { global $birchschedule; $ns->init = function () use($ns, $birchschedule) { $birchschedule->model->pre_save->when($ns->is_model_appointment, $ns->pre_save); $birchschedule->model->post_get->when($ns->is_model_appointment, $ns->post_get); }; $ns->is_model_appointment = function ($model) { return $model['post_type'] === 'birs_appointment'; }; $ns->pre_save = function ($appointment, $config) { birch_assert(is_array($appointment) && isset($appointment['post_type'])); global $birchschedule; if (isset($appointment['_birs_appointment_duration'])) { $appointment['_birs_appointment_duration'] = (int) $appointment['_birs_appointment_duration']; } return $appointment; }; $ns->post_get = function ($appointment) { birch_assert(is_array($appointment) && isset($appointment['post_type'])); global $birchpress; if (isset($appointment['_birs_appointment_timestamp'])) { $timestamp = $appointment['_birs_appointment_timestamp']; $appointment['_birs_appointment_datetime'] = $birchpress->util->convert_to_datetime($timestamp); } if (!isset($appointment['appointment1on1s'])) { $appointment['appointment1on1s'] = array(); } $appointment['_birs_appointment_admin_url'] = admin_url(sprintf('post.php?post=%s&action=edit', $appointment['ID'])); return $appointment; }; });
$birchschedule->model->post_get->when($ns->is_model_service, $ns->post_get); }; $ns->is_model_service = function ($model) { return $model['post_type'] === 'birs_service'; }; $ns->pre_save = function ($service, $config) { if (isset($service['_birs_service_pre_payment_fee'])) { $service['_birs_service_pre_payment_fee'] = serialize($service['_birs_service_pre_payment_fee']); } if (isset($service['_birs_assigned_staff'])) { $service['_birs_assigned_staff'] = serialize($service['_birs_assigned_staff']); } return $service; }; $ns->post_get = function ($service) { birch_assert(is_array($service) && isset($service['post_type'])); if (isset($service['_birs_service_pre_payment_fee'])) { $service['_birs_service_pre_payment_fee'] = unserialize($service['_birs_service_pre_payment_fee']); if (!$service['_birs_service_pre_payment_fee']) { $service['_birs_service_pre_payment_fee'] = array(); } } if (isset($service['_birs_assigned_staff'])) { $service['_birs_assigned_staff'] = unserialize($service['_birs_assigned_staff']); if (!$service['_birs_assigned_staff']) { $service['_birs_assigned_staff'] = array(); } } if (isset($service['post_title'])) { $service['_birs_service_name'] = $service['post_title']; }
$definitions = get_option('birchpress_db_associations', array()); if (isset($definitions[$association_fullname]) || isset($definitions[$reverse_fullname])) { throw new ErrorException(sprintf('%s or %s has been defined.', $association_fullname, $reverse_fullname)); } $association_tax = $ns->new_association_taxonomy(); $reverse_tax = $ns->new_association_taxonomy(); $definitions[$association_fullname] = array('post_type' => $association_def['post_type'], 'association' => $association_def['association'], 'taxonomy' => $association_tax, 'reverse' => $reverse_fullname); $definitions[$reverse_fullname] = array('post_type' => $reverse['post_type'], 'association' => $reverse['association'], 'taxonomy' => $reverse_tax, 'reverse' => $association_fullname); update_option('birchpress_db_associations', $definitions); }; $ns->alter_association = function ($association_def, $new_assocation_def) use($ns) { birch_assert(isset($association_def['post_type']), 'Association should has post_type.'); birch_assert(isset($association_def['association']), 'Association should has a name.'); birch_assert(isset($new_assocation_def['post_type']), 'New association should has post_type.'); birch_assert(isset($new_assocation_def['association']), 'New association should has a name.'); birch_assert($association_def['post_type'] === $new_assocation_def['post_type'], 'post_type of association cannot be altered.'); $definitions = get_option('birchpress_db_associations', array()); $association_fullname = $ns->get_association_fullname($association_def['post_type'], $association_def['association']); $new_association_fullname = $ns->get_association_fullname($new_assocation_def['post_type'], $new_assocation_def['association']); if (!isset($definitions[$association_fullname])) { throw new ErrorException(sprintf('%s has not been defined.', $association_fullname)); } $definitions[$new_association_fullname] = $definitions[$association_fullname]; $definitions[$new_association_fullname]['association'] = $new_assocation_def['association']; $reverse_fullname = $definitions[$association_fullname]['reverse']; $definitions[$reverse_fullname]['reverse'] = $new_association_fullname; unset($definitions[$association_fullname]); update_option('birchpress_db_associations', $definitions); }; $ns->get_association_fullname = function ($post_type, $association) { return implode('/', array($post_type, $association));