action = ATKPTools::get_get_parameter( 'action', 'string' );
$this->templateid = ATKPTools::get_get_parameter( 'templateid', 'string' );
$this->templatename = ATKPTools::get_get_parameter( 'templatename', 'string' );
if ( $this->action == '' ) {
$this->action = ATKPTools::get_post_parameter( 'action', 'string' );
}
$this->saving = ATKPTools::get_post_parameter( 'saving', 'bool' );
if ( $this->templateid == '' ) {
$this->templateid = ATKPTools::get_post_parameter( 'templateid', 'string' );
}
add_action( 'atkp_register_submenu', array( &$this, 'register_admin_menu' ), 10, 1 );
if ( $this->action == '' ) {
add_filter( 'set-screen-option', [ &$this, 'set_screen' ], 10, 3 );
}
}
public function set_screen( $status, $option, $value ) {
return $value;
}
private $atkp_template_table;
public function register_admin_menu( $parentmenu ) {
global $submenu;
$hook = add_submenu_page(
$parentmenu,
esc_html__( 'Templates', 'affiliate-toolkit-starter' ),
esc_html__( 'Templates', 'affiliate-toolkit-starter' ),
//a template body is compiled and executed, so template management belongs to
//administrators - show_page() enforces the same capability
'manage_options',
ATKP_PLUGIN_PREFIX . '_viewtemplate',
array( &$this, 'show_page' )
);
if ( $this->action == '' ) {
add_action( "load-$hook", [ $this, 'screen_option' ] );
}
}
public function screen_option() {
$option = 'per_page';
$args = [
'label' => 'Templates',
'default' => 50,
'option' => 'templates_per_page'
];
add_screen_option( $option, $args );
$this->atkp_template_table = new atkp_template_table();
}
private function import_template( $contents, $template_name = '', $regenerate_styles = true ) {
try {
$mytemplate = json_decode( $contents );
if ( $mytemplate === null && json_last_error() !== JSON_ERROR_NONE ) {
// Fallback zu unserialize wenn JSON decode fehlschlägt
$mytemplate = @unserialize( $contents );
if ( $mytemplate === false ) {
return 'template not readable: invalid format';
}
}
if ( isset( $mytemplate->data ) ) {
$mytemplate = $mytemplate->data;
}
$post_id = null;
if ( $mytemplate == null ) {
return 'template not readable: ' . $contents;
} else if ( isset( $mytemplate->fields ) ) {
$fields = array_keys( get_object_vars( $mytemplate->fields ) );
$my_post = array(
'post_title' => $mytemplate->template_name,
'post_type' => ATKP_TEMPLATE_POSTTYPE,
'post_status' => 'publish',
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// Fields that contain template content and must be sanitized
$content_fields = array( '_body', '_header', '_body_header', '_body_footer', '_detail_header', '_detail_footer', '_footer' );
foreach ( $fields as $field ) {
//the keys come from the uploaded file. The export only ever writes the atkp_
//meta keys of the template, so everything else is skipped instead of being
//created as an arbitrary meta key on the new post.
if ( substr( $field, 0, 5 ) !== 'atkp_' ) {
continue;
}
$unval = is_array( $mytemplate->fields->$field ) ? ( count( $mytemplate->fields->$field ) > 0 ? $mytemplate->fields->$field[0] : null ) : $mytemplate->fields->$field;
if ( $unval != null ) {
$data = @unserialize( $unval );
if ( $data !== false ) {
$unval = $data;
}
}
// Sanitize template content fields to prevent code injection
if ( is_string( $unval ) && ! atkp_options::$loader->get_disable_template_sanitize() ) {
foreach ( $content_fields as $cf ) {
if ( substr( $field, -strlen( $cf ) ) === $cf ) {
$unval = atkp_template_helper::sanitize_template_content( $unval );
break;
}
}
}
//die Werte stammen aus der hochgeladenen Datei und sind ungeslasht,
//update_post_meta() unslasht aber selbst - siehe set_post_setting()
update_post_meta( $post_id, $field, wp_slash( $unval ) );
}
} else {
if ( $mytemplate['template_type'] == '' ) {
$mytemplate['template_type'] = 6;
}
$my_post = array(
'post_title' => $template_name == '' ? $mytemplate['title'] : $template_name,
'post_type' => ATKP_TEMPLATE_POSTTYPE,
'post_status' => 'publish',
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_disabledisclaimer', $mytemplate['disableddisclaimer'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_extendedview', $mytemplate['extendedview'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_template_type', $mytemplate['template_type'] );
// Sanitize: strip raw PHP tags from all template content fields to prevent code injection
$template_content_fields = array( 'header', 'bodyheader', 'detailheader', 'detailfooter', 'body', 'bodyfooter', 'footer' );
$template_meta_keys = array( '_header', '_body_header', '_detail_header', '_detail_footer', '_body', '_body_footer', '_footer' );
foreach ( $template_content_fields as $idx => $field ) {
$value = isset( $mytemplate[ $field ] ) ? $mytemplate[ $field ] : '';
if ( ! atkp_options::$loader->get_disable_template_sanitize() ) {
$value = atkp_template_helper::sanitize_template_content( $value );
}
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . $template_meta_keys[ $idx ], $value );
}
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_css', $mytemplate['css'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_comparevalues', $mytemplate['comparevalues'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_horizontalscrollbars', $mytemplate['horizontalscrollbars'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_hideheaders', $mytemplate['hideheaders'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_maxmobileproducts', $mytemplate['maxmobileproducts'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_maxproducts', $mytemplate['maxproducts'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_viewtype', $mytemplate['viewtype'] );
ATKPTools::set_post_setting( $post_id, ATKP_TEMPLATE_POSTTYPE . '_mobilebody', $mytemplate['mobilebody'] );
$imported = true;
}
if ( $regenerate_styles ) {
ATKPTools::write_global_scripts();
ATKPTools::write_global_styles();
}
wp_safe_redirect( admin_url( 'post.php?action=edit&post=' . $post_id ) );
return $post_id;
} catch ( Exception $e ) {
return 'Unknown content!
' . 'Fehler: ' . $e->getMessage();
}
return null;
}
public function show_page() {
// Restrict template management to administrators only
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'affiliate-toolkit-starter' ) );
}
$importmessage = '';
if ( $this->action == '' || $this->action == 'import' ) {
$delete_nonce = wp_create_nonce( 'atkp_edit_queue' );
$imported = false;
if ( ATKPTools::exists_post_parameter( 'saveimporttemplate' ) && check_admin_referer( 'save', 'save' ) ) {
//reads the name of the file the user submitted for uploading
$templatefile = isset( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['name'] ) ? sanitize_file_name( wp_unslash( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['name'] ) ) : '';
//if it is not empty
if ( $templatefile ) {
//get the original name of the file from the clients machine
$filename = stripslashes( isset( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['name'] ) ? sanitize_file_name( wp_unslash( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['name'] ) ) : '' );
//get the extension of the file in a lower case format
$extension = pathinfo( $filename, PATHINFO_EXTENSION );
$extension = strtolower( $extension );
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//we will only allow .ttf and .otf file extensions
//otherwise we will do more tests
if ( $extension != "txt" && $extension != "json" ) {
//print error message
$importmessage = 'Unknown fileextension!';
$errors = 1;
} else {
$tmp_name = isset( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['tmp_name'] ) ? sanitize_text_field( wp_unslash( $_FILES[ ATKP_PLUGIN_PREFIX . '_filetemplate' ]['tmp_name'] ) ) : '';
$contents = file_get_contents( $tmp_name ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$importmessage = $this->import_template( $contents );
if ( is_numeric( $importmessage ) ) {
echo '';
}
}
} else {
$importmessage = 'file not found';
}
}
?>
' . esc_html( $new_post_id ) . '
'; } } else { $args = array( 'post_status' => 'draft', 'post_title' => $this->templatename . ' (2)', 'post_type' => ATKP_TEMPLATE_POSTTYPE, ); /* * insert the post by wp_insert_post() function */ $new_post_id = wp_insert_post( $args ); if ( is_numeric( $this->templateid ) ) { global $wpdb; //custom template: only a real template may be cloned. The id used to be taken //as given, so the meta of any post could be copied into a new atkp_template. $source_template = ATKPTools::get_template_post( $this->templateid ); if ( $source_template == null ) { wp_delete_post( $new_post_id, true ); echo 'not allowed'; exit; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $post_meta_infos = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d", $source_template->ID ) ); foreach ( $post_meta_infos as $meta_info ) { if ( $meta_info->meta_key == '_wp_old_slug' ) { continue; } //this used to be one INSERT ... UNION ALL SELECT built by hand, with the //meta key interpolated unescaped and the value run through addslashes(). //add_post_meta() unslashes its input, so the raw database value has to be //slashed to be stored byte identical. add_post_meta( $new_post_id, $meta_info->meta_key, wp_slash( $meta_info->meta_value ) ); } } else { //systemtemplate $mytemplate = apply_filters( 'atkp_template_get_blade', '', $this->templateid ); $mycss = apply_filters( 'atkp_template_get_css', '', $this->templateid ); $mytemplatetype = apply_filters( 'atkp_template_get_type', '6', $this->templateid ); ATKPTools::set_post_setting( $new_post_id, 'atkp_template_template_type', $mytemplatetype ); ATKPTools::set_post_setting( $new_post_id, 'atkp_template_css', $mycss ); ATKPTools::set_post_setting( $new_post_id, 'atkp_template_body', $mytemplate ); } echo ''; //wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ); } } else if ( $this->action == 'delete' ) { $nounce = ATKPTools::get_get_parameter( '_wpnonce', 'string' ); if ( ! wp_verify_nonce( $nounce, 'atkp_edit_template' ) ) { echo( 'Nonce expired. Please reload page.' ); exit; } if ( ! current_user_can( 'manage_options' ) ) { echo( 'User has no permission.' ); exit; } if ( get_post_type( $this->templateid ) == ATKP_TEMPLATE_POSTTYPE ) { wp_delete_post( $this->templateid ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce already verified above. $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : ''; echo ''; exit; } else { echo 'not allowed'; } } } }