importer = new Importer(); } /** * Retry partial and untranslated imported rules. * * @param array|null $widget_ids Limit to these widget IDs, or null for all. * @param int $user_id Acting user. * @return array{ok:bool, error?:string, retried?:int, improved?:int, skipped?:array} */ public function retry( $widget_ids, $user_id ) { $lock = $this->importer->lock(); if ( ! $lock ) { return array( 'ok' => false, 'error' => 'busy', ); } try { $translator = new Translator(); $rank = array( 'untranslated' => 0, 'partial' => 1, 'translated_pattern' => 2, 'translated' => 3, ); $retried = 0; $improved = 0; $skipped = array(); $only = is_array( $widget_ids ) ? array_flip( array_map( 'strval', $widget_ids ) ) : null; foreach ( Widget_Store::all_rules() as $wid => $raw ) { if ( null !== $only && ! isset( $only[ $wid ] ) ) { continue; } $rule = Rule::from_stored( $raw ); $imp = ( Rule::STATE_OK === $rule->state && is_array( $rule->import ) ) ? $rule->import : null; if ( ! $imp || ! isset( $imp['status'], $imp['source'] ) || ! in_array( $imp['status'], array( 'partial', 'untranslated' ), true ) ) { continue; } if ( ! empty( $imp['edited'] ) ) { $skipped[] = array( 'widget_id' => $wid, 'why' => 'edited_kept', ); continue; } $tr = $translator->translate( (string) $imp['source'] ); $new = Rule::from_tree( $tr['tree'] ); $new->import = $imp; $new->import['status'] = $tr['status']; $new->import['reasons'] = $tr['reasons']; $new->import['risk'] = $tr['risk']; $new->import['risk_terms'] = $tr['risk_terms']; $still_unsure = ! in_array( $tr['status'], array( 'translated', 'translated_pattern' ), true ); // An override survives only on a location row that still has unknown parts. if ( 'audience' === $tr['risk'] || ! $still_unsure ) { $new->import['override'] = null; } $new->import['retried_at'] = time(); $prepared = Rule_Saver::prepare( $new, $rule, 'widget', (int) $user_id ); if ( Widget_Store::write_rule( $wid, $prepared->to_array() ) ) { ++$retried; if ( $rank[ $tr['status'] ] > $rank[ $imp['status'] ] ) { ++$improved; } } } return array( 'ok' => true, 'retried' => $retried, 'improved' => $improved, 'skipped' => $skipped, ); } finally { $this->importer->unlock( $lock ); } } /** * Undo the import. * * @param array $include_edited Widget IDs of edited imported rules the admin ticked for removal. * @return array{ok:bool, error?:string, removed?:int, kept_edited?:array, settings_restored?:array} */ public function undo( array $include_edited ) { $lock = $this->importer->lock(); if ( ! $lock ) { return array( 'ok' => false, 'error' => 'busy', ); } try { $tick = array_flip( array_map( 'strval', $include_edited ) ); $removed = 0; $kept = array(); foreach ( Widget_Store::all_rules() as $wid => $raw ) { $rule = Rule::from_stored( $raw ); if ( Rule::STATE_OK !== $rule->state || ! is_array( $rule->import ) ) { continue; // Not an imported rule: never touched by Undo. } if ( ! empty( $rule->import['edited'] ) && ! isset( $tick[ $wid ] ) ) { $kept[] = $wid; continue; } if ( Widget_Store::write_rule( $wid, null ) ) { ++$removed; } } $report = $this->importer->read_report(); $restored = array(); if ( isset( $report['imported_settings'] ) && is_array( $report['imported_settings'] ) ) { $allowed = array( 'main_query_fix' ); $changes = array(); foreach ( $report['imported_settings'] as $k => $v ) { if ( in_array( $k, $allowed, true ) ) { $changes[ $k ] = (bool) $v; $restored[] = $k; } } if ( $changes ) { Settings::update( $changes ); } } $report['imported_settings'] = array(); $report['undone'] = array( 'at' => time(), 'removed' => $removed, 'kept_edited' => $kept, ); unset( $report['lock'] ); update_option( Importer::REPORT_OPTION, $report, false ); return array( 'ok' => true, 'removed' => $removed, 'kept_edited' => $kept, 'settings_restored' => $restored, ); } finally { $this->importer->unlock( $lock ); } } /** * What Remove would delete, and what is not archived (dry check). * * @return array{locations:array, not_archived:array, options:array, options_not_archived:array, block_attr_left:int} */ public function remove_plan() { $scan = ( new Scanner() )->scan( null ); $locations = array(); $not_archived = array(); foreach ( $scan['strings'] as $s ) { $locations[] = array( 'ref' => $s['ref'], 'widget_id' => $s['widget_id'], 'location' => $s['location'], ); if ( ! Legacy_Archive::holds( $s['ref'], $s['source'] ) ) { $not_archived[] = array( 'ref' => $s['ref'], 'source' => $s['source'], ); } } $archive = Legacy_Archive::read(); $opt_known = array(); foreach ( $archive['legacy_options'] as $o ) { $opt_known[ $o['name'] . '|' . $o['hash'] ] = true; } $options = array(); $opt_missing = array(); foreach ( array( Scanner::LEGACY_OPTION, Scanner::LEGACY_SETTINGS ) as $name ) { $value = get_option( $name, null ); if ( null === $value ) { continue; } $options[] = $name; $hash = hash( 'sha256', serialize( $value ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- hashing only, matches Legacy_Archive. if ( ! isset( $opt_known[ $name . '|' . $hash ] ) ) { $opt_missing[] = $name; } } return array( 'locations' => $locations, 'not_archived' => $not_archived, 'options' => $options, 'options_not_archived' => $opt_missing, 'block_attr_left' => count( $scan['block_attr_rules'] ), 'truncated' => (bool) $scan['truncated'], ); } /** * Remove Widget Logic data (permanent). * * @param string $confirm Typed confirmation; must equal CONFIRM_PHRASE. * @return array{ok:bool, error?:string, removed_keys?:int, removed_options?:array, not_archived?:array} */ public function remove_legacy( $confirm ) { if ( ! is_string( $confirm ) || self::CONFIRM_PHRASE !== $confirm ) { return array( 'ok' => false, 'error' => 'confirmation_mismatch', ); } if ( ! Legacy_Detector::allows( 'apply' ) ) { return array( 'ok' => false, 'error' => 'legacy_plugin_active', ); } $report = $this->importer->read_report(); if ( empty( $report['applied']['ok'] ) ) { return array( 'ok' => false, 'error' => 'no_apply_yet', ); } $lock = $this->importer->lock(); if ( ! $lock ) { return array( 'ok' => false, 'error' => 'busy', ); } try { $plan = $this->remove_plan(); if ( ! empty( $plan['truncated'] ) ) { // R-3: a partial scan cannot vouch that everything was archived. return array( 'ok' => false, 'error' => 'scan_truncated', ); } if ( $plan['not_archived'] || $plan['options_not_archived'] ) { return array( 'ok' => false, 'error' => 'not_archived', 'not_archived' => $plan['not_archived'], 'options_not_archived' => $plan['options_not_archived'], ); } $removed = 0; foreach ( $plan['locations'] as $loc ) { if ( in_array( $loc['location'], array( 'instance', 'block_widget' ), true ) ) { $removed += $this->unset_instance_key( $loc['widget_id'] ) ? 1 : 0; } elseif ( 'old_style' === $loc['location'] ) { $removed += $this->unset_old_style_key( $loc['widget_id'] ) ? 1 : 0; } // legacy_option strings go with the whole option below. } $gone = array(); foreach ( $plan['options'] as $name ) { if ( delete_option( $name ) ) { $gone[] = $name; } } $report = $this->importer->read_report(); $report['removed'] = array( 'at' => time(), 'keys' => $removed, 'options' => $gone, 'block_attr_left' => $plan['block_attr_left'], ); unset( $report['lock'] ); update_option( Importer::REPORT_OPTION, $report, false ); return array( 'ok' => true, 'removed_keys' => $removed, 'removed_options' => $gone, 'block_attr_left' => $plan['block_attr_left'], ); } finally { $this->importer->unlock( $lock ); } } /** * Unset widget_logic in a numbered instance. * * @param string $widget_id Widget ID. * @return bool */ private function unset_instance_key( $widget_id ) { $loc = Widget_Store::locate( $widget_id ); if ( null === $loc || null === $loc['number'] ) { return false; } wp_cache_delete( $loc['option'], 'options' ); $opt = get_option( $loc['option'] ); if ( ! is_array( $opt ) || ! isset( $opt[ $loc['number'] ] ) || ! is_array( $opt[ $loc['number'] ] ) || ! array_key_exists( 'widget_logic', $opt[ $loc['number'] ] ) ) { return false; } unset( $opt[ $loc['number'] ]['widget_logic'] ); return update_option( $loc['option'], $opt ); } /** * Unset the top-level widget_logic key of an old-style widget option. * * @param string $widget_id Widget ID (the option is widget_). * @return bool */ private function unset_old_style_key( $widget_id ) { if ( ! is_string( $widget_id ) || ! preg_match( '/^[A-Za-z0-9_\-]+$/', $widget_id ) ) { return false; } $name = 'widget_' . $widget_id; wp_cache_delete( $name, 'options' ); $opt = get_option( $name ); if ( ! is_array( $opt ) || ! array_key_exists( 'widget_logic', $opt ) ) { return false; } unset( $opt['widget_logic'] ); return update_option( $name, $opt ); } }