ring $table_name Table name. * @param string $index_name Index name, empty string for the primary key. * @return array The index columns. Empty array if the table or the index don't exist. */ public function get_index_columns( string $table_name, string $index_name = '' ): array { global $wpdb; if ( empty( $index_name ) ) { $index_name = 'PRIMARY'; } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $results = $wpdb->get_results( $wpdb->prepare( "SHOW INDEX FROM $table_name WHERE Key_name = %s", $index_name ) ); if ( empty( $results ) ) { return array(); } return array_column( $results, 'Column_name' ); } /** * Formats an object value of type `$type` for inclusion in the database. * * @param mixed $value Raw value. * @param string $type Data type. * @return mixed * @throws \Exception When an invalid type is passed. */ public function format_object_value_for_db( $value, string $type ) { switch ( $type ) { case 'decimal': $value = wc_format_decimal( $value, false, true ); break; case 'int': $value = (int) $value; break; case 'bool': $value = wc_string_to_bool( $value ); break; case 'string': $value = strval( $value ); break; case 'date': // Date properties are converted to the WP timezone (see WC_Data::set_date_prop() method), however // for our own tables we persist dates in GMT. $value = $value ? ( new DateTime( $value ) )->setTimezone( new DateTimeZone( '+00:00' ) )->format( 'Y-m-d H:i:s' ) : null; break; case 'date_epoch': $value = $value ? ( new DateTime( "@{$value}" ) )->format( 'Y-m-d H:i:s' ) : null; break; default: throw new \Exception( 'Invalid type received: ' . $type ); } return $value; } /** * Returns the `$wpdb` placeholder to use for data type `$type`. * * @param string $type Data type. * @return string * @throws \Exception When an invalid type is passed. */ public function get_wpdb_format_for_type( string $type ) { static $wpdb_placeholder_for_type = array( 'int' => '%d', 'decimal' => '%f', 'string' => '%s', 'date' => '%s', 'date_epoch' => '%s', 'bool' => '%d', ); if ( ! isset( $wpdb_placeholder_for_type[ $type ] ) ) { throw new \Exception( 'Invalid column type: ' . $type ); } return $wpdb_placeholder_for_type[ $type ]; } /** * Generates ON DUPLICATE KEY UPDATE clause to be used in migration. * * @param array $columns List of column names. * * @return string SQL clause for INSERT...ON DUPLICATE KEY UPDATE */ public function generate_on_duplicate_statement_clause( array $columns ): string { $update_value_statements = array(); foreach ( $columns as $column ) { $update_value_statements[] = "`$column` = VALUES( `$column` )"; } $update_value_clause = implode( ', ', $update_value_statements ); return "ON DUPLICATE KEY UPDATE $update_value_clause"; } /** * Hybrid of $wpdb->update and $wpdb->insert. It will try to update a row, and if it doesn't exist, it will insert it. This needs unique constraints to be set on the table on all ID columns. * * You can use this function only when: * 1. There is only one unique constraint on the table. The constraint can contain multiple columns, but it must be the only one unique constraint. * 2. The complete unique constraint must be part of the $data array. * 3. You do not need the LAST_INSERT_ID() value. * * @param string $table_name Table name. * @param array $data Unescaped data to update (in column => value pairs). * @param array $format An array of formats to be mapped to each of the values in $data. * * @return int Returns the value of DB's ON DUPLICATE KEY UPDATE clause. */ public function insert_on_duplicate_key_update( $table_name, $data, $format ) : int { global $wpdb; if ( empty( $data ) ) { return 0; } $columns = array_keys( $data ); $value_format = array(); $values = array(); $index = 0; // Directly use NULL for placeholder if the value is NULL, since otherwise $wpdb->prepare will convert it to empty string. foreach ( $data as $key => $value ) { if ( is_null( $value ) ) { $value_format[] = 'NULL'; } else { $values[] = $value; $value_format[] = $format[ $index ]; } $index++; } $column_clause = '`' . implode( '`, `', $columns ) . '`'; $value_format_clause = implode( ', ', $value_format ); $on_duplicate_clause = $this->generate_on_duplicate_statement_clause( $columns ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- Values are escaped in $wpdb->prepare. $sql = $wpdb->prepare( " INSERT INTO $table_name ( $column_clause ) VALUES ( $value_format_clause ) $on_duplicate_clause ", $values ); // phpcs:enable // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is prepared. return $wpdb->query( $sql ); } /** * Get max index length. * * @return int Max index length. */ public function get_max_index_length() : int { /** * Filters the maximum index length in the database. * * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that. * As of WP 4.2, however, they moved to utf8mb4, which uses 4 bytes per character. This means that an index which * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters. * * Additionally, MyISAM engine also limits the index size to 1000 bytes. We add this filter so that interested folks on InnoDB engine can increase the size till allowed 3071 bytes. * * @param int $max_index_length Maximum index length. Default 191. * * @since 8.0.0 */ $max_index_length = apply_filters( 'woocommerce_database_max_index_length', 191 ); // Index length cannot be more than 768, which is 3078 bytes in utf8mb4 and max allowed by InnoDB engine. return min( absint( $max_index_length ), 767 ); } } get_formatted(); } } $this->restore_storing_document_settings(); return $data; } public function generate_document_shortcode( $atts, $content = null, $shortcode_tag = '' ) { global $wp; if ( is_admin() ) { return; } // Default values $values = shortcode_atts( array( 'order_id' => '', 'link_text' => '', 'id' => '', 'class' => 'wpo_wcpdf_document_link', 'document_type' => 'invoice', ), $atts ); $is_document_type_valid = false; $documents = WPO_WCPDF()->documents->get_documents(); foreach ( $documents as $document ) { if ( $document->get_type() === $values['document_type'] ) { $is_document_type_valid = true; if ( ! empty( $values['link_text'] ) ) { $link_text = $values['link_text']; } else { $link_text = sprintf( /* translators: %s: Document type */ __( 'Download %s (PDF)', 'woocommerce-pdf-invoices-packing-slips' ), wp_kses_post( $document->get_type() ) ); } break; } } if ( ! $is_document_type_valid ) { return; } // Get $order if ( empty( $values['order_id'] ) ) { if ( is_checkout() && is_wc_endpoint_url( 'order-received' ) && isset( $wp->query_vars['order-received'] ) ) { $order = wc_get_order( $wp->query_vars['order-received'] ); } elseif ( is_account_page() && is_wc_endpoint_url( 'view-order' ) && isset( $wp->query_vars['view-order'] ) ) { $order = wc_get_order( $wp->query_vars['view-order'] ); } } else { $order = wc_get_order( $values['order_id'] ); } if ( empty( $order ) || ! is_object( $order ) ) { return; } $document = wcpdf_get_document( $values['document_type'], $order ); if ( ! $document || ! $document->is_allowed() ) { return; } $pdf_url = WPO_WCPDF()->endpoint->get_document_link( $order, $values['document_type'], [ 'shortcode' => 'true' ] ); if ( 'wcpdf_document_link' === $shortcode_tag ) { return esc_url( $pdf_url ); } return sprintf( '

%s

', ( ! empty( $values['id'] ) ? 'id="' . esc_attr( $values['id'] ) . '"' : '' ), esc_attr( $values['class'] ), esc_url( $pdf_url ), esc_html( $link_text ) ); } /** * Document objects are created in order to check for existence and retrieve data, * but we don't want to store the settings for uninitialized documents. * Only use in frontend/backed (page requests), otherwise settings will never be stored! */ public function disable_storing_document_settings() { add_filter( 'wpo_wcpdf_document_store_settings', array( $this, 'return_false' ), 9999 ); } public function restore_storing_document_settings() { remove_filter( 'wpo_wcpdf_document_store_settings', array( $this, 'return_false' ), 9999 ); } public function return_false(){ return false; } } endif; // class_exists