Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/Api/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,7 @@ public function testConnection(): array {
$workspace->isSeatLive()
);

if ( ! $workspace->isSeatLive() ) {
$result = array(
'ok' => false,
'message' => __( 'This workspace has no active Verifycate plan. Activate a plan before issuing certificates.', 'verifycate' ),
'total' => 0,
);
} elseif ( ! $workspace->canWriteCertificates() ) {
if ( ! $workspace->canWriteCertificates() ) {
$result = array(
'ok' => false,
'message' => __( 'This API key can read certificates but cannot issue them. Create a key with the certificates:write scope and reconnect.', 'verifycate' ),
Expand Down Expand Up @@ -195,7 +189,7 @@ private function describeApiError( ApiException $exception ): string {
}

if ( $exception->isPaymentRequired() ) {
return __( 'This workspace needs an active Verifycate plan before certificates can be issued.', 'verifycate' );
return __( 'This workspace needs more credits before certificates can be issued, or the action uses a Pro feature.', 'verifycate' );
}

if ( $exception->isForbidden() ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Frontend/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public function assets(): void {
array(),
$version
);

wp_enqueue_style( 'verifycate-wallet' );
}

/**
Expand Down Expand Up @@ -168,6 +166,8 @@ public function renderBlock( array $attributes ): string {
* @return string
*/
public function render( array $args ): string {
wp_enqueue_style( 'verifycate-wallet' );

$title = isset( $args['title'] ) && '' !== (string) $args['title'] ? (string) $args['title'] : __( 'My certificates', 'verifycate' );
$empty_text = isset( $args['empty_text'] ) && '' !== (string) $args['empty_text'] ? (string) $args['empty_text'] : __( 'No certificates yet.', 'verifycate' );
$limit = isset( $args['limit'] ) ? max( 1, min( 50, (int) $args['limit'] ) ) : 20;
Expand Down
16 changes: 8 additions & 8 deletions src/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ private static function migrateMappingTriggers(): void {

$table = $wpdb->prefix . 'verifycate_mappings';

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "UPDATE {$table} SET event = 'course_completed' WHERE event = ''" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( $wpdb->prepare( "UPDATE %i SET event = 'course_completed' WHERE event = ''", $table ) );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$index = $wpdb->get_results( "SHOW INDEX FROM {$table} WHERE Key_name = 'mapping'", ARRAY_A );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$index = $wpdb->get_results( $wpdb->prepare( 'SHOW INDEX FROM %i WHERE Key_name = %s', $table, 'mapping' ), ARRAY_A );
$keys = array();

foreach ( is_array( $index ) ? $index : array() as $row ) {
Expand All @@ -172,12 +172,12 @@ private static function migrateMappingTriggers(): void {
}

if ( $keys !== array() ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "ALTER TABLE {$table} DROP INDEX mapping" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( $wpdb->prepare( 'ALTER TABLE %i DROP INDEX mapping', $table ) );
}

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "ALTER TABLE {$table} ADD UNIQUE KEY mapping (integration,event,trigger_id,course_id,certificate_id)" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD UNIQUE KEY mapping (integration,event,trigger_id,course_id,certificate_id)', $table ) );
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Integrations/LearnPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ private function getTriggerItems( string $post_type, int $limit ): array {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names are built from $wpdb->prefix, values are prepared.
"SELECT p.ID, p.post_title, s.section_course_id AS course_id FROM {$wpdb->posts} p INNER JOIN {$items} si ON si.item_id = p.ID INNER JOIN {$sections} s ON s.section_id = si.section_id WHERE p.post_type = %s AND p.post_status = 'publish' GROUP BY p.ID, p.post_title, s.section_course_id ORDER BY s.section_course_id, p.post_title LIMIT %d",
"SELECT p.ID, p.post_title, s.section_course_id AS course_id FROM %i p INNER JOIN %i si ON si.item_id = p.ID INNER JOIN %i s ON s.section_id = si.section_id WHERE p.post_type = %s AND p.post_status = 'publish' GROUP BY p.ID, p.post_title, s.section_course_id ORDER BY s.section_course_id, p.post_title LIMIT %d",
$wpdb->posts,
$items,
$sections,
$post_type,
max( 1, min( 1000, $limit ) )
),
Expand Down
55 changes: 31 additions & 24 deletions src/Issuance/MappingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ public function all( bool $enabled_only = false ): array {
global $wpdb;

$table = $this->table();
$sql = "SELECT * FROM {$table}";

if ( $enabled_only ) {
$sql .= ' WHERE enabled = 1';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE enabled = 1 ORDER BY id DESC', $table ), ARRAY_A );
} else {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i ORDER BY id DESC', $table ), ARRAY_A );
}

$sql .= ' ORDER BY id DESC';

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$rows = $wpdb->get_results( $sql, ARRAY_A );

return $this->hydrate( is_array( $rows ) ? $rows : array() );
}

Expand All @@ -75,13 +73,18 @@ public function paginate( int $page = 1, int $per_page = 20, string $search = ''
list( $where, $params ) = $this->searchClause( $search );

$order_sql = $this->orderClause( $orderby, $order );
$sql = "SELECT m.* FROM {$table} m LEFT JOIN {$posts} p ON p.ID = m.course_id {$where} {$order_sql} LIMIT %d OFFSET %d";
$params[] = $per_page;
$params[] = $offset;
$prepared = $wpdb->prepare( $sql, $params );
$params = array_merge( array( $table, $posts ), $params, array( $per_page, $offset ) );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
$rows = $wpdb->get_results( $prepared, ARRAY_A );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- WHERE clause is static; values use placeholders.
$rows = $wpdb->get_results(
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Search placeholders are built dynamically.
$wpdb->prepare(
"SELECT m.* FROM %i m LEFT JOIN %i p ON p.ID = m.course_id {$where} {$order_sql} LIMIT %d OFFSET %d",
$params
),
ARRAY_A
);

return $this->hydrate( is_array( $rows ) ? $rows : array() );
}
Expand All @@ -96,8 +99,8 @@ public function find( int $id ): ?Mapping {
global $wpdb;

$table = $this->table();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ), ARRAY_A );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ), ARRAY_A );

return is_array( $row ) ? Mapping::fromRow( $row ) : null;
}
Expand Down Expand Up @@ -206,10 +209,11 @@ public function matching( string $integration, int $course_id, string $event, in
global $wpdb;

$table = $this->table();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$table} WHERE enabled = 1 AND integration = %s AND (course_id = 0 OR course_id = %d) AND event = %s AND (trigger_id = 0 OR trigger_id = %d) ORDER BY course_id DESC, trigger_id DESC, id ASC",
'SELECT * FROM %i WHERE enabled = 1 AND integration = %s AND (course_id = 0 OR course_id = %d) AND event = %s AND (trigger_id = 0 OR trigger_id = %d) ORDER BY course_id DESC, trigger_id DESC, id ASC',
$table,
$integration,
$course_id,
$event,
Expand All @@ -235,14 +239,17 @@ public function count( string $search = '' ): int {

list( $where, $params ) = $this->searchClause( $search );

$sql = "SELECT COUNT(*) FROM {$table} m LEFT JOIN {$posts} p ON p.ID = m.course_id {$where}";

if ( $params !== array() ) {
$sql = $wpdb->prepare( $sql, $params );
}
$args = array_merge( array( $table, $posts ), $params );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
return (int) $wpdb->get_var( $sql );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- WHERE clause is static; values use placeholders.
return (int) $wpdb->get_var(
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Search placeholders are built dynamically.
$wpdb->prepare(
"SELECT COUNT(*) FROM %i m LEFT JOIN %i p ON p.ID = m.course_id {$where}",
$args
)
);
}

/**
Expand Down
53 changes: 30 additions & 23 deletions src/Issuance/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,20 @@ public function claim( int $limit = 10 ): array {
$now = current_time( 'mysql', true );
$stale = gmdate( 'Y-m-d H:i:s', time() - self::LOCK_TIMEOUT );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"UPDATE {$table} SET status = 'pending', locked_at = NULL WHERE status = 'processing' AND locked_at < %s",
'UPDATE %i SET status = \'pending\', locked_at = NULL WHERE status = \'processing\' AND locked_at < %s',
$table,
$stale
)
);

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT id FROM {$table} WHERE status = 'pending' AND available_at <= %s ORDER BY id ASC LIMIT %d",
'SELECT id FROM %i WHERE status = \'pending\' AND available_at <= %s ORDER BY id ASC LIMIT %d',
$table,
$now,
$limit
)
Expand All @@ -154,10 +156,11 @@ public function claim( int $limit = 10 ): array {
$jobs = array();

foreach ( is_array( $ids ) ? $ids : array() as $id ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$updated = $wpdb->query(
$wpdb->prepare(
"UPDATE {$table} SET status = 'processing', locked_at = %s, attempts = attempts + 1, updated_at = %s WHERE id = %d AND status = 'pending'",
'UPDATE %i SET status = \'processing\', locked_at = %s, attempts = attempts + 1, updated_at = %s WHERE id = %d AND status = \'pending\'',
$table,
$now,
$now,
(int) $id
Expand All @@ -168,9 +171,9 @@ public function claim( int $limit = 10 ): array {
continue;
}

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$row = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", (int) $id ),
$wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, (int) $id ),
ARRAY_A
);

Expand Down Expand Up @@ -228,8 +231,8 @@ public function fail( int $id, string $error, bool $retryable, ?int $retry_after
global $wpdb;

$table = $this->table();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$attempts = (int) $wpdb->get_var( $wpdb->prepare( "SELECT attempts FROM {$table} WHERE id = %d", $id ) );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$attempts = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT attempts FROM %i WHERE id = %d', $table, $id ) );

if ( $retryable && $attempts < self::MAX_ATTEMPTS ) {
$delay = self::retryDelay( $attempts, $retry_after );
Expand Down Expand Up @@ -291,10 +294,11 @@ public function retryFailed(): int {
$table = $this->table();
$now = current_time( 'mysql', true );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$updated = $wpdb->query(
$wpdb->prepare(
"UPDATE {$table} SET status = 'pending', attempts = 0, available_at = %s, locked_at = NULL, updated_at = %s WHERE status = 'failed'",
'UPDATE %i SET status = \'pending\', attempts = 0, available_at = %s, locked_at = NULL, updated_at = %s WHERE status = \'failed\'',
$table,
$now,
$now
)
Expand All @@ -319,10 +323,11 @@ public function retry( int $id ): bool {
$table = $this->table();
$now = current_time( 'mysql', true );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$updated = $wpdb->query(
$wpdb->prepare(
"UPDATE {$table} SET status = 'pending', attempts = 0, available_at = %s, locked_at = NULL, last_error = NULL, updated_at = %s WHERE id = %d",
'UPDATE %i SET status = \'pending\', attempts = 0, available_at = %s, locked_at = NULL, last_error = NULL, updated_at = %s WHERE id = %d',
$table,
$now,
$now,
$id
Expand All @@ -348,8 +353,8 @@ public function counts(): array {

$table = $this->table();
$rows = $wpdb->get_results(
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT status, COUNT(*) AS total FROM {$table} GROUP BY status",
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare( 'SELECT status, COUNT(*) AS total FROM %i GROUP BY status', $table ),
ARRAY_A
);

Expand Down Expand Up @@ -382,9 +387,9 @@ public function recent( int $limit = 50 ): array {
$table = $this->table();
$limit = max( 1, min( 200, $limit ) );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$rows = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM {$table} ORDER BY id DESC LIMIT %d", $limit ),
$wpdb->prepare( 'SELECT * FROM %i ORDER BY id DESC LIMIT %d', $table, $limit ),
ARRAY_A
);

Expand Down Expand Up @@ -428,10 +433,11 @@ public function purgeOld( int $days = 30 ): int {
$table = $this->table();
$cutoff = gmdate( 'Y-m-d H:i:s', time() - ( max( 1, $days ) * DAY_IN_SECONDS ) );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$deleted = $wpdb->query(
$wpdb->prepare(
"DELETE FROM {$table} WHERE status IN ('done','failed') AND updated_at < %s",
'DELETE FROM %i WHERE status IN (\'done\',\'failed\') AND updated_at < %s',
$table,
$cutoff
)
);
Expand All @@ -455,12 +461,13 @@ private function countByStatus( string $dedupe_key, array $statuses ): int {

$table = $this->table();
$placeholders = implode( ',', array_fill( 0, count( $statuses ), '%s' ) );
$args = array_merge( array( $dedupe_key ), $statuses );
$args = array_merge( array( $table, $dedupe_key ), $statuses );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$count = $wpdb->get_var(
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Status placeholders are built dynamically.
$wpdb->prepare(
"SELECT COUNT(*) FROM {$table} WHERE dedupe_key = %s AND status IN ({$placeholders})",
"SELECT COUNT(*) FROM %i WHERE dedupe_key = %s AND status IN ({$placeholders})",
$args
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Issuance/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private function describeApiError( ApiException $exception ): string {
$detail = $exception->getDetail();

if ( $exception->isPaymentRequired() ) {
return __( 'The workspace needs an active plan before certificates can be issued.', 'verifycate' );
return __( 'The workspace needs more credits before certificates can be issued.', 'verifycate' );
}

if ( $exception->isUnauthorized() ) {
Expand Down
Loading
Loading