= $this->get_meta_date( $id, 'start_date' ); if ( empty( $start_date ) ) { // If no meta start date is set, use subscription date. $start_date = clone $subscription->date; } $subscription->start_date = $start_date; // End Date. $end_date = $this->get_meta_date( $id, 'end_date' ); if ( empty( $end_date ) && null !== $subscription->frequency && null !== $date_interval ) { // @link https://stackoverflow.com/a/10818981/6411283 $period = new DatePeriod( $start_date, $date_interval, $subscription->frequency ); $dates = iterator_to_array( $period ); $end_date = end( $dates ); } $subscription->end_date = $end_date; // Expiry Date. $expiry_date = $this->get_meta_date( $id, 'expiry_date' ); if ( empty( $expiry_date ) && null !== $date_interval ) { // If no meta expiry date is set, use start date + 1 interval period. $expiry_date = clone $start_date; $expiry_date->add( $date_interval ); } $subscription->expiry_date = $expiry_date; // Next Payment Date. $subscription->next_payment_date = $this->get_meta_date( $id, 'next_payment' ); // Next Payment Delivery Date. $subscription->next_payment_delivery_date = $this->get_meta_date( $id, 'next_payment_delivery_date' ); if ( empty( $subscription->next_payment_delivery_date ) && null !== $subscription->next_payment_date ) { $subscription->next_payment_delivery_date = clone $subscription->next_payment_date; } // Legacy. parent::read_post_meta( $subscription ); } /** * Update payment post meta. * * @link https://github.com/woocommerce/woocommerce/blob/3.2.6/includes/data-stores/class-wc-order-data-store-cpt.php#L154-L257 * @param Subscription $subscription The subscription to update the post meta for. * @return void */ private function update_post_meta( $subscription ) { $id = $subscription->get_id(); if ( empty( $id ) ) { return; } $customer = $subscription->get_customer(); $this->update_meta( $id, 'config_id', $subscription->config_id ); $this->update_meta( $id, 'key', $subscription->key ); $this->update_meta( $id, 'source', $subscription->source ); $this->update_meta( $id, 'source_id', $subscription->source_id ); $this->update_meta( $id, 'frequency', $subscription->frequency ); $this->update_meta( $id, 'interval', $subscription->interval ); $this->update_meta( $id, 'interval_period', $subscription->interval_period ); $this->update_meta( $id, 'description', $subscription->description ); $this->update_meta( $id, 'email', ( null === $customer ? null : $customer->get_email() ) ); $this->update_meta( $id, 'customer_name', ( null === $customer ? null : strval( $customer->get_name() ) ) ); $this->update_meta( $id, 'payment_method', $subscription->payment_method ); $this->update_meta( $id, 'start_date', $subscription->start_date ); $this->update_meta( $id, 'end_date', $subscription->end_date ); $this->update_meta( $id, 'expiry_date', $subscription->expiry_date ); $this->update_meta( $id, 'next_payment', $subscription->next_payment_date ); $this->update_meta( $id, 'next_payment_delivery_date', $subscription->next_payment_delivery_date ); $display_phase = $subscription->get_display_phase(); if ( null !== $display_phase ) { $this->update_meta( $id, 'currency', $display_phase->get_amount()->get_currency()->get_alphabetic_code() ); $this->update_meta( $id, 'amount', $display_phase->get_amount()->format() ); } $this->update_meta_status( $subscription ); } /** * Update meta status. * * @param Subscription $subscription The subscription to update the status for. * @return void */ public function update_meta_status( $subscription ) { $id = $subscription->get_id(); if ( empty( $id ) ) { return; } $previous_status = $this->get_meta( $id, 'status' ); $this->update_meta( $id, 'status', $subscription->status ); if ( $previous_status !== $subscription->status ) { $old = $previous_status; $old = empty( $old ) ? 'unknown' : $old; $old = strtolower( $old ); $new = $subscription->status; $new = empty( $new ) ? 'unknown' : $new; $new = strtolower( $new ); $can_redirect = false; do_action( 'pronamic_subscription_status_update_' . $subscription->source . '_' . $old . '_to_' . $new, $subscription, $can_redirect, $previous_status, $subscription->status ); do_action( 'pronamic_subscription_status_update_' . $subscription->source, $subscription, $can_redirect, $previous_status, $subscription->status ); do_action( 'pronamic_subscription_status_update', $subscription, $can_redirect, $previous_status, $subscription->status ); } } }