post_content; $scode = strpos($cont, '[reservify_booking]')!==FALSE; if(!$status || !$scode) { $upd = true; } } else { $ins = true; } } else { $ins = true; } if($ins) { $pid = wp_insert_post([ 'post_type' => 'page', 'post_title' => 'Reserva online', 'post_content' => '[reservify_booking]', 'post_excerpt' => 'Reserva online', 'post_status' => 'publish', ]); if(is_wp_error($pid)) { throw new Exception($pid->get_error_message()); } else { update_option('reservify_booking_pid', $pid); } } if($upd) { // $cont = $page ? $page->post_content : null; $ok = wp_update_post([ 'ID' => $pid, 'post_content' => '[reservify_booking]', // .PHP_EOL.$cont 'post_status' => 'publish', ]); if(is_wp_error($ok)) { throw new Exception($ok->get_error_message()); } } $pid = get_option('reservify_thankyou_pid'); if($pid) { $page = get_post($pid); if($page) { $status = $post_status=='publish'; $cont = $page->post_content; $scode = strpos($cont, '[reservify_thankyou]')!==FALSE; if(!$status || !$scode) { $upd = true; } } else { $ins = true; } } else { $ins = true; } if($ins) { $pid = wp_insert_post([ 'post_type' => 'page', 'post_title' => 'Reserva online : Gracias por tu reserva', 'post_content' => '[reservify_thankyou]', 'post_excerpt' => 'Gracias por tu reserva online', 'post_status' => 'publish', ]); if(is_wp_error($pid)) { throw new Exception($pid->get_error_message()); } else { update_option('reservify_thankyou_pid', $pid); } } if($upd) { // $cont = $page ? $page->post_content : null; $ok = wp_update_post([ 'ID' => $pid, 'post_content' => '[reservify_thankyou]', // .PHP_EOL.$cont 'post_status' => 'publish', ]); if(is_wp_error($ok)) { throw new Exception($ok->get_error_message()); } } $term = term_exists('reservas', 'product_cat'); if(empty($term)) { $term = wp_insert_term('reservas', 'product_cat'); update_option('reservify_booking_cid', $term['term_id']); } else { update_option('reservify_booking_cid', $term['term_id']); } } static public function get_permalink($service) { $pid = get_option('reservify_booking_pid'); $url = get_permalink($pid); if(strpos($url, '?')) { $url .= '&service='.$service; } else { $url .= '?service='.$service; } return $url; } private $flag_payed = false; // as the same method is attached to several hooks we need a flag to avoid redoing. public function __construct() { add_action('init', [$this, 'on_init'], 1000); add_action('wp_head', [$this, 'render_header'], 1000); // add_action('wp_footer', [$this, 'render_footer'], 1000); add_shortcode('reservify_booking', [$this, 'render_booking']); add_shortcode('reservify_thankyou', [$this, 'render_thankyou']); add_action('woocommerce_payment_complete', [$this, 'payment_completed'], 10, 1); add_action('woocommerce_payment_complete_order_status_completed', [$this, 'payment_completed'], 10, 1); add_action('woocommerce_payment_complete_order_status_processing', [$this, 'payment_completed'], 10, 1); add_action('woocommerce_order_status_pending_to_completed_notification', [$this, 'payment_completed'], 10, 1); add_action('woocommerce_order_status_pending_to_processing_notification', [$this, 'payment_completed'], 10, 1); add_action('woocommerce_order_status_failed_to_processing_notification', [$this, 'payment_completed'], 10, 1); add_action( 'add_meta_boxes', function () { add_meta_box( 'reservify', 'Reservify', [$this, 'mbox_reservify'], 'shop_order', 'side', 'core' ); }); add_action('wp_ajax_payment_completed', function () { $order_id = $_POST['order_id'] ?? null; $this->payment_completed($order_id); die('Ok'); }); } public function on_init() { require_once(ABSPATH.'reservify/ssi/common.php'); } public function render_header() { $pid = get_option('reservify_booking_pid'); $pid = apply_filters( 'wpml_object_id', $pid, 'page', true, ICL_LANGUAGE_CODE); if(is_page($pid)) { ?>