forked from Tokinx/Wing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page-links.php
executable file
·214 lines (212 loc) · 10.3 KB
/
page-links.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/*
Template Name: 友情链接
*/
get_header();
?>
<article class="article" itemscope itemtype="https://schema.org/Article">
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
require_once( "inc/article-header.php" ); ?>
<div class="article-content" itemprop="articleBody">
<ul id="<?= ( is_super_admin() ? 'Links' : '' ) ?>"
class="article-cards article-links columns reset-ul">
<?php the_friendly_links(); ?>
</ul>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
?>
</article>
<!--判断是否登录-->
<?php if ( is_super_admin() ): ?>
<script>
// 友情链接设置
window.LinkSettingDialog = () => {
const Dialog = Vue.extend({
template: `
<div class="modal active modal-links">
<a href="javascript: void(0);" class="modal-overlay" @click="hide" aria-label="Close"></a>
<div class="modal-container uni-shadow" style="max-width: 980px;">
<div class="modal-header">
<a href="javascript: void(0);" class="btn btn-clear text-gray float-right" @click="hide" aria-label="Close"></a>
<div class="modal-title h5 text-gray">{{ $lang.translate('Link Settings') }}</div>
</div>
<form method="post" action @submit="e => e.preventDefault()">
<div class="modal-body article wp-block-table is-style-stripes" ref="body">
<div v-if="loading" class="loading" style="position: absolute;inset: 0;z-index: 1;"></div>
<table class="table table-scroll" :style="{ opacity: loading ? 0.3 : 1, height: '100%' }">
<thead>
<tr><th v-for="col of tableColumn" :key="col.prop" v-bind="col.bind">{{ col.name }}</th></tr>
</thead>
<tbody ref="DragSort">
<tr v-for="(rows, index) of links" :key="index" :class="['text-tiny', { drag: !rows.edit }]">
<td v-for="{ name, prop, slot } of tableColumn" :key="prop">
<template v-if="!slot">
<span v-if="!rows.edit">{{ rows[prop] || "" }}</span>
<input v-else class="form-input" v-model="rows[prop]" :placeholder="name" />
</template>
<template v-else>
<template v-if="prop === 'operate'">
<button class="btn btn-link btn-sm" @click="handleEditLink(index)">{{ rows.edit ? $lang.translate('Confirm') : $lang.translate('Edit') }}</button>
<button class="btn btn-link btn-sm text-error" @click="handleRemoveLink(index)">{{ $lang.translate('Delete') }}</button>
</template>
<template v-else-if="prop === 'drag-handle'">
<div v-show="!rows.edit" class="drag-handle dashicons dashicons-menu c-move"></div>
</template>
</template>
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<div class="btn btn-link float-left" @click="handleAddLinks">{{ $lang.translate('Add Links') }}</div>
<button class="btn btn-primary" :disabled="loading" @click="submit">{{ $lang.translate('Save All') }}</button>
</div>
</form>
</div>
</div>
`,
data() {
return {
post_id: <?php the_ID();?>,
loading: false,
links: [],
tableColumn: [
{
name: '',
prop: 'drag-handle',
slot: true,
bind: {
style: { width: '50px', minWidth: '50px' }
}
},
// {
// name: '图像',
// prop: 'image',
// bind: {
// style: { minWidth: '120px' }
// }
// },
{
name: $lang.translate('Name'),
prop: 'name',
bind: {
style: { minWidth: '200px' }
}
},
{
name: $lang.translate('Url'),
prop: 'url',
bind: {
style: { minWidth: '240px' }
}
},
{
name: $lang.translate('Description'),
prop: 'description',
bind: {
style: { width: '100%', minWidth: '240px' }
}
},
{
name: $lang.translate('Operate'),
prop: 'operate',
slot: true,
bind: {
style: { minWidth: '120px' }
}
},
],
};
},
created() {
this.getLinks();
},
mounted() {
const $table = this.$refs.DragSort;
new Sortable($table, {
draggable: ".drag",
handle: ".drag-handle",
onUpdate: ({ oldIndex, newIndex }) => {
const $new = $table.children[newIndex];
const $old = $table.children[oldIndex];
$table.removeChild($new);
if ( newIndex > oldIndex ) {
$table.insertBefore($new, $old);
} else {
$table.insertBefore($new, $old.nextSibling);
}
const item = this.links.splice(oldIndex, 1);
this.links.splice(newIndex, 0, item[0]);
},
});
},
methods: {
getLinks() {
this.loading = true;
$h.ajax({ query: { action: 'get_post_meta', post_id: this.post_id, key: 'links' } })
.then(({ data }) => {
this.links = JSON.parse(data);
})
.finally(() => {
this.loading = false;
})
},
submit(e) {
if ( this.links.some(item => (!item.name || !item.url)) ) return;
this.links = this.links.map(item => {
delete item.edit;
return item;
});
this.loading = true;
$h.ajax({
query: { action: 'submit_post_meta', 'post_id': this.post_id, 'key': 'links' },
data: { content: JSON.stringify(this.links) },
method: 'POST',
})
.then((html) => {
this.reRender(html);
this.$toast({ type: 'success', message: $lang.translate('Successfully') });
this.hide();
})
.finally(() => {
this.loading = false;
});
e.preventDefault();
},
reRender(html) {
document.querySelector("#Links").innerHTML = html;
},
handleAddLinks() {
this.links.push({ edit: true });
this.$nextTick(() => {
const el = this.$refs.body;
el.scrollTop = el.scrollHeight;
});
},
handleEditLink(index) {
this.$set(this.links, index, { ...this.links[index], edit: !this.links[index].edit });
},
handleRemoveLink(index) {
this.links.splice(index, 1);
},
hide() {
this.$el.remove();
},
},
});
const vm = new Dialog({ el: document.createElement('div') });
document.querySelector('#main').appendChild(vm.$el);
};
</script>
<?php endif;
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
get_footer();
?>